- Notifications
You must be signed in to change notification settings - Fork1.4k
troposphere - Python library to create AWS CloudFormation descriptions
License
cloudtools/troposphere
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
troposphere - library to createAWS CloudFormation descriptions
The troposphere library allows for easier creation of theAWS CloudFormationJSON by writing Python code to describe the AWS resources. troposphere alsoincludes some basic support forOpenStack resources via Heat.
To facilitate catching CloudFormation or JSON errors early the library hasproperty and type checking built into the classes.
troposphere can be installed using the pip distribution system for Python byissuing:
$ pip install troposphere
To install troposphere withawacs(recommended soft dependency):
$ pip install troposphere[policy]
Alternatively, you can use setup.py to install by cloning this repositoryand issuing:
$ python setup.py install# you may need sudo depending on your python installation
A simple example to create an instance would look like this:
>>>fromtroposphereimportRef,Template>>>importtroposphere.ec2asec2>>>t=Template()>>>instance=ec2.Instance("myinstance")>>>instance.ImageId="ami-951945d0">>>instance.InstanceType="t1.micro">>>t.add_resource(instance)<troposphere.ec2.Instanceobjectat0x101bf3390>>>>print(t.to_json()){"Resources": {"myinstance": {"Properties": {"ImageId":"ami-951945d0","InstanceType":"t1.micro" },"Type":"AWS::EC2::Instance" } }}>>>print(t.to_yaml())Resources:myinstance:Properties:ImageId:ami-951945d0InstanceType:t1.microType: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.Instanceobjectat0x101bf3550>
Andadd_resource()
returns the object to make it easy to use withRef()
:
>>>instance=t.add_resource(ec2.Instance("myinstance",ImageId="ami-951945d0",InstanceType="t1.micro"))>>>Ref(instance)<troposphere.Refobjectat0x101bf3490>
Incorrect property being set on AWS resource:
>>>importtroposphere.ec2asec2>>>ec2.Instance("ec2instance",image="i-XXXX")Traceback (mostrecentcalllast):...AttributeError:AWS::EC2::Instanceobjectdoesnotsupportattributeimage
Incorrect type for AWS resource property:
>>>ec2.Instance("ec2instance",ImageId=1)Traceback (mostrecentcalllast):...TypeError:ImageIdis<type'int'>,expected<type'basestring'>
Missing required property for the AWS resource:
>>>fromtroposphereimportTemplate>>>importtroposphere.ec2asec2>>>t=Template()>>>t.add_resource(ec2.Subnet("ec2subnet",VpcId="vpcid"))<troposphere.ec2.Subnetobjectat0x100830ed0>>>>print(t.to_json())Traceback (mostrecentcalllast):...ValueError:ResourceCidrBlockrequiredintypeAWS::EC2::Subnet (title:ec2subnet)
# Converted from EC2InstanceSample.template located at:# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/fromtroposphereimportBase64,FindInMap,GetAttfromtroposphereimportParameter,Output,Ref,Templateimporttroposphere.ec2asec2template=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())
We have a Google Group,cloudtools-dev, where you can ask questions andengage with the troposphere community. Issues and pull requests are alwayswelcome!
troposphere is licensed under theBSD 2-Clause license.SeeLICENSE for the troposphere full license text.
About
troposphere - Python library to create AWS CloudFormation descriptions