troposphère - bibliothèque pour créer des descriptions AWS CloudFormation
La bibliothèque troposphère permet de créer plus facilement le JSON AWS CloudFormation en écrivant du code Python pour décrire les ressources AWS. troposphere inclut également une prise en charge de base des ressources OpenStack via Heat.
Pour faciliter la détection précoce des erreurs CloudFormation ou JSON, la bibliothèque intègre une vérification des propriétés et des types dans les classes.
la troposphère peut être installée à l'aide du système de distribution pip pour Python en émettant :
$ pip install troposphere
Pour installer la troposphère avec awacs (dépendance logicielle recommandée) :
$ pip install troposphere[policy]
Alternativement, vous pouvez utiliser setup.py pour installer en clonant ce référentiel et en émettant :
$ python setup.py install # you may need sudo depending on your python installation
Un exemple simple pour créer une instance ressemblerait à ceci :
> >> 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"
}
}
}
> >> print ( t . to_yaml ())
Resources :
myinstance :
Properties :
ImageId : ami - 951945 d0
InstanceType : t1 . micro
Type : AWS :: EC2 :: Instance
Alternativement, les paramètres peuvent être utilisés à la place des propriétés :
> >> instance = ec2 . Instance ( "myinstance" , ImageId = "ami-951945d0" , InstanceType = "t1.micro" )
> >> t . add_resource ( instance )
< troposphere . ec2 . Instance object at 0x101bf3550 >
Et add_resource()
renvoie l'objet pour le rendre facile à utiliser avec Ref()
:
> >> instance = t . add_resource ( ec2 . Instance ( "myinstance" , ImageId = "ami-951945d0" , InstanceType = "t1.micro" ))
> >> Ref ( instance )
< troposphere . Ref object at 0x101bf3490 >
Propriété incorrecte définie sur la ressource AWS :
> >> 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
Type incorrect pour la propriété de ressource AWS :
> >> ec2 . Instance ( "ec2instance" , ImageId = 1 )
Traceback ( most recent call last ):
...
TypeError : ImageId is < type 'int' > , expected < type 'basestring' >
Propriété obligatoire manquante pour la ressource AWS :
> >> from troposphere import Template
> >> import troposphere . ec2 as ec2
> >> t = Template ()
> >> t . add_resource ( ec2 . Subnet ( "ec2subnet" , VpcId = "vpcid" ))
< troposphere . ec2 . Subnet object at 0x100830ed0 >
> >> print ( t . to_json ())
Traceback ( most recent call last ):
...
ValueError : Resource CidrBlock required in type AWS :: EC2 :: Subnet ( title : ec2subnet )
# 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 ())
Nous avons un groupe Google, cloudtools-dev, où vous pouvez poser des questions et interagir avec la communauté de la troposphère. Les problèmes et les demandes de tirage sont toujours les bienvenus !
troposphère est sous licence BSD 2-Clause. Voir LICENCE pour le texte complet de la licence troposphère.