Coder Social home page Coder Social logo

cclauss / troposphere Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudtools/troposphere

0.0 1.0 0.0 2.18 MB

troposphere - Python library to create AWS CloudFormation descriptions

License: BSD 2-Clause "Simplified" License

Makefile 0.21% Python 99.78% Shell 0.01%

troposphere's Introduction

troposphere

PyPI Version Build Status license: New BSD license Documentation Status

About

troposphere - library to create AWS CloudFormation descriptions

The troposphere library allows for easier creation of the AWS CloudFormation JSON by writing Python code to describe the AWS resources. troposphere also includes some basic support for OpenStack resources via Heat.

To facilitate catching CloudFormation or JSON errors early the library has property and type checking built into the classes.

Installation

troposphere can be installed using the pip distribution system for Python by issuing:

$ pip install troposphere

To install troposphere with awacs (recommended soft dependency):

$ pip install troposphere[policy]

Alternatively, you can run use setup.py to install by cloning this repository and issuing:

$ python setup.py install  # you may need sudo depending on your python installation

Examples

A simple example to create an instance would look like this:

>>> from troposphere import Ref, Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> instance = ec2.Instance("myinstance")
>>> instance.ImageId = "ami-951945d0"
>>> instance.InstanceType = "t1.micro"
>>> t.add_resource(instance)
<troposphere.ec2.Instance object at 0x101bf3390>
>>> print(t.to_json())
{
    "Resources": {
        "myinstance": {
            "Properties": {
                "ImageId": "ami-951945d0",
                "InstanceType": "t1.micro"
            },
            "Type": "AWS::EC2::Instance"
        }
    }
}

A simple example to create an instance (YAML) would look like this:

>>> from troposphere import Ref, Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> instance = ec2.Instance("myinstance")
>>> instance.ImageId = "ami-951945d0"
>>> instance.InstanceType = "t1.micro"
>>> t.add_resource(instance)
<troposphere.ec2.Instance object at 0x101bf3390>
>>> print(t.to_yaml())

Resources:
    myinstance:
        Properties:
            ImageId: ami-951945d0
            InstanceType: t1.micro
        Type: AWS::EC2::Instance

Alternatively, parameters can be used instead of properties:

>>> instance = ec2.Instance("myinstance", ImageId="ami-951945d0", InstanceType="t1.micro")
>>> t.add_resource(instance)
<troposphere.ec2.Instance object at 0x101bf3550>

And add_resource() returns the object to make it easy to use with Ref():

>>> instance = t.add_resource(ec2.Instance("myinstance", ImageId="ami-951945d0", InstanceType="t1.micro"))
>>> Ref(instance)
<troposphere.Ref object at 0x101bf3490>

Examples of the error checking (full tracebacks removed for clarity):

Incorrect property being set on AWS resource:

>>> import troposphere.ec2 as ec2
>>> ec2.Instance("ec2instance", image="i-XXXX")
Traceback (most recent call last):
...
AttributeError: AWS::EC2::Instance object does not support attribute image

Incorrect type for AWS resource property:

>>> ec2.Instance("ec2instance", ImageId=1)
Traceback (most recent call last):
...
TypeError: ImageId is <type 'int'>, expected <type 'basestring'>

Missing required property for the AWS resource:

>>> from troposphere import Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> t.add_resource(ec2.Instance("ec2instance", InstanceType="m3.medium"))
<troposphere.ec2.Instance object at 0x109ee2e50>
>>> print(t.to_json())
Traceback (most recent call last):
...
ValueError: Resource ImageId required in type AWS::EC2::Instance

Currently supported AWS resource types

  • AWS::AmazonMQ
  • AWS::Amplify
  • AWS::ApiGateway
  • AWS::ApiGatewayV2
  • AWS::AppMesh
  • AWS::AppStream
  • AWS::AppSync
  • AWS::ApplicationAutoScaling
  • AWS::Athena
  • AWS::AutoScaling
  • AWS::AutoScalingPlans
  • AWS::Batch
  • AWS::Budgets
  • AWS::CertificateManager
  • AWS::Cloud9
  • AWS::CloudFormation
  • AWS::CloudFront
  • AWS::CloudTrail
  • AWS::CloudWatch
  • AWS::CodeBuild
  • AWS::CodeCommit
  • AWS::CodeDeploy
  • AWS::CodePipeline
  • AWS::Cognito
  • AWS::Config
  • AWS::DAX
  • AWS::DLM
  • AWS::DMS
  • AWS::DataPipeline
  • AWS::DirectoryService
  • AWS::DocDB
  • AWS::DynamoDB
  • AWS::EC2
  • AWS::ECR
  • AWS::ECS
  • AWS::EFS
  • AWS::EKS
  • AWS::EMR
  • AWS::ElastiCache
  • AWS::ElasticBeanstalk
  • AWS::ElasticLoadBalancing
  • AWS::ElasticLoadBalancingV2
  • AWS::Elasticsearch
  • AWS::Events
  • AWS::FSx
  • AWS::Glue
  • AWS::Greengrass
  • AWS::GuardDuty
  • AWS::IAM
  • AWS::Inspector
  • AWS::IoT
  • AWS::IoT1Click
  • AWS::IoTAnalytics
  • AWS::KMS
  • AWS::Kinesis
  • AWS::KinesisAnalytics
  • AWS::KinesisAnalyticsV2
  • AWS::KinesisFirehose
  • AWS::Lambda
  • AWS::Logs
  • AWS::MediaLive
  • AWS::MediaStore
  • AWS::MSK
  • AWS::Neptune
  • AWS::OpsWorks
  • AWS::OpsWorksCM
  • AWS::PinpointEmail
  • AWS::RAM
  • AWS::RDS
  • AWS::Redshift
  • AWS::RoboMaker
  • AWS::Route53
  • AWS::Route53Resolver
  • AWS::S3
  • AWS::SDB
  • AWS::SES
  • AWS::SNS
  • AWS::SQS
  • AWS::SSM
  • AWS::SageMaker
  • AWS::SecretsManager
  • AWS::SecurityHub
  • AWS::Serverless
  • AWS::ServiceCatalog
  • AWS::ServiceDiscovery
  • AWS::StepFunctions
  • AWS::Transfer
  • AWS::WAF
  • AWS::WAFRegional
  • AWS::WorkSpaces

Currently supported OpenStack resource types

  • OS::Neutron::Firewall
  • OS::Neutron::FirewallPolicy
  • OS::Neutron::FirewallRule
  • OS::Neutron::FloatingIP
  • OS::Neutron::FloatingIPAssociation
  • OS::Neutron::HealthMonitor
  • OS::Neutron::Pool
  • OS::Neutron::LoadBalancer
  • OS::Neutron::Net
  • OS::Neutron::PoolMember
  • OS::Neutron::Port
  • OS::Neutron::SecurityGroup
  • OS::Nova::FloatingIP
  • OS::Nova::FloatingIPAssociation
  • OS::Nova::KeyPair
  • OS::Nova::Server

Todo:

  • Add additional validity checks

Duplicating a single instance sample would look like this

# Converted from EC2InstanceSample.template located at:
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/

from troposphere import Base64, FindInMap, GetAtt
from troposphere import Parameter, Output, Ref, Template
import troposphere.ec2 as ec2


template = Template()

keyname_param = template.add_parameter(Parameter(
    "KeyName",
    Description="Name of an existing EC2 KeyPair to enable SSH "
                "access to the instance",
    Type="String",
))

template.add_mapping('RegionMap', {
    "us-east-1":      {"AMI": "ami-7f418316"},
    "us-west-1":      {"AMI": "ami-951945d0"},
    "us-west-2":      {"AMI": "ami-16fd7026"},
    "eu-west-1":      {"AMI": "ami-24506250"},
    "sa-east-1":      {"AMI": "ami-3e3be423"},
    "ap-southeast-1": {"AMI": "ami-74dda626"},
    "ap-northeast-1": {"AMI": "ami-dcfa4edd"}
})

ec2_instance = template.add_resource(ec2.Instance(
    "Ec2Instance",
    ImageId=FindInMap("RegionMap", Ref("AWS::Region"), "AMI"),
    InstanceType="t1.micro",
    KeyName=Ref(keyname_param),
    SecurityGroups=["default"],
    UserData=Base64("80")
))

template.add_output([
    Output(
        "InstanceId",
        Description="InstanceId of the newly created EC2 instance",
        Value=Ref(ec2_instance),
    ),
    Output(
        "AZ",
        Description="Availability Zone of the newly created EC2 instance",
        Value=GetAtt(ec2_instance, "AvailabilityZone"),
    ),
    Output(
        "PublicIP",
        Description="Public IP address of the newly created EC2 instance",
        Value=GetAtt(ec2_instance, "PublicIp"),
    ),
    Output(
        "PrivateIP",
        Description="Private IP address of the newly created EC2 instance",
        Value=GetAtt(ec2_instance, "PrivateIp"),
    ),
    Output(
        "PublicDNS",
        Description="Public DNSName of the newly created EC2 instance",
        Value=GetAtt(ec2_instance, "PublicDnsName"),
    ),
    Output(
        "PrivateDNS",
        Description="Private DNSName of the newly created EC2 instance",
        Value=GetAtt(ec2_instance, "PrivateDnsName"),
    ),
])

print(template.to_json())

Community

We have a Google Group, cloudtools-dev, where you can ask questions and engage with the troposphere community. Issues and pull requests are always welcome!

Licensing

troposphere is licensed under the BSD 2-Clause license. See LICENSE for the troposphere full license text.

troposphere's People

Contributors

acmcelwee avatar andybotting avatar andystanton avatar axelpavageau avatar benbridts avatar bmurtagh avatar bobveznat avatar chrisgilmerproj avatar edubxb avatar flou avatar fungusakafungus avatar gkrizek avatar hltbra avatar horacio3 avatar iblazevic avatar jcoleman avatar jeanphix avatar jonapich avatar markpeek avatar mbrossard avatar michael-k avatar mwildehahn avatar nielslaukens avatar oussemos avatar pas256 avatar philtay avatar phobologic avatar quiver avatar rcuza avatar stevengorrell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.