Package: tropostack
AboutTropostack is a CLI/workflow library that simplifies the creation and management
ofCloudFormationstacks, based on
the excellentTroposphere Project.Tropostack features:Single stack template = single executable Python file = single CLISupport for different configuration and CLI pluginsA collection of generic commands available to each stack (e.g.create)Support for user-defined CLI commands (e.g.upscale)Helper routines (e.g. locate the newest matching AMI)DocsFull docs are athttps://tropostack.readthedocs.io/en/latest/Installation$pipinstalltropostackOr, you can use setup.py to install from a cloned repository:$pythonsetup.pyinstallFirst stackYou usetropostackas a library to:Consisteny define CloudFormation templates in Python codeHave a CLI around each stack definition, enabling it to live as a standalone executableHere is a minimalistic example of a stack that creates an S3 bucket, and exports the ARN as an Output:#!/usr/bin/env python3fromtroposphereimports3fromtroposphereimportOutput,Export,Sub,GetAttfromtropostack.baseimportInlineConfStackfromtropostack.cliimportInlineConfCLIclassMyS3BucketStack(InlineConfStack):# Name of the stackBASE_NAME='my-s3-bucket-stack'# Define configuration values for the stackCONF={# Region is always explicitly required'region':'eu-west-1',# Prefix the bucket name with the account ID'bucket_name':Sub('${AWS::AccountId}-my-first-tropostack-bucket')}# Stack Resources are defined as class properties prefixed with 'r_'@propertydefr_bucket(self):returns3.Bucket('MyBucketResource',BucketName=self.conf['bucket_name'])# Stack Outputs are defined as class properties prefixed with 'o_'@propertydefo_bucket_arn(self):_id='BucketArn'returnOutput(_id,Description='The ARN of the S3 bucket',Value=GetAtt(self.r_bucket,'Arn'),# We're exporting the output as <StackName>-<OutputId># Other stacks can read the output relying on the same conventionExport=Export(Sub("${AWS::StackName}-%s"%_id)))if__name__=='__main__':# Wrap the stack in a CLI and run itcli=InlineConfCLI(MyS3BucketStack)cli.run()The above already gives you a usable CLI around your stack definition.Assuming you put it inside an executable file calleds3_minimal.py, you’d be able to call it already:$./s3_minimal.py-husage:s3_minimal.py[-h]{apply,create,delete,outputs,print,update,validate}positionalarguments:{apply,create,delete,outputs,print,update,validate}optionalarguments:-h,--helpshowthishelpmessageandexitYou can now inspect the “raw” CloudFormation code generated by the stack:$ ./s3_minimal.py printOutputs:BucketArn:Description:The ARN of the S3 bucketExport:Name:!Sub'${AWS::StackName}-BucketArn'Value:!GetAtt'MyBucketResource.Arn'Resources:MyBucketResource:Properties:BucketName:!Sub'${AWS::AccountId}-my-first-tropostack-bucket'Type:AWS::S3::BucketAssumingAWS credentials are presentin the environment, we can now fire up stack that would create our S3 bucket:$./s3_minimal.pycreateStackcreationinitiatedfor:arn:aws:cloudformation:eu-west-1:472799024263:stack/my-s3-bucket-stack/dd5e93c0-225c-11ea-93d8-0641c159a77aTIMESTAMP(UTC)RESOURCETYPERESOURCEIDSTATUSREASON2019-12-1912:41:23AWS::CloudFormation::Stackmy-s3-bucket-stackCREATE_IN_PROGRESSUserInitiated2019-12-1912:41:26AWS::S3::BucketMyBucketResourceCREATE_IN_PROGRESS2019-12-1912:41:27AWS::S3::BucketMyBucketResourceCREATE_IN_PROGRESSResourcecreationInitiated2019-12-1912:41:48AWS::S3::BucketMyBucketResourceCREATE_COMPLETE2019-12-1912:41:50AWS::CloudFormation::Stackmy-s3-bucket-stackCREATE_COMPLETEWe can also inspect the stack Outputs - in this case, the ARN of the bucket:$./s3_minimal.pyoutputsStackisinstatus:CREATE_COMPLETEOutputKeyOutputValueDescriptionExportName-------------------------------------------------------------------------------------------------------------------BucketArnarn:aws:s3:::472799024263-my-first-tropostack-bucketTheARNoftheS3bucketmy-s3-bucket-stack-BucketArnFinally, we can clean up and have our stack deleted:$./s3_minimal.pydeleteDestroyinitiatedforstack:my-s3-bucket-stackTIMESTAMP(UTC)RESOURCETYPERESOURCEIDSTATUSREASON2019-12-1912:44:59AWS::CloudFormation::Stackmy-s3-bucket-stackDELETE_IN_PROGRESSUserInitiatedStackisgone:my-s3-bucket-stack(Anerroroccurred(ValidationError)whencallingtheDescribeStackEventsoperation:Stack[my-s3-bucket-stack]doesnotexist)Stock commandsWhile the CLI can be expanded/customized for each individual tropostack, there are several subcommands that come out of the box:print- prints the resulting CloudFormation YAML to the screenvalidate- Sends the CloudFormation template to the AWS API for validation, and reports back resultcreate- Initiates the stack creation (should only be used if the stack does not exist yet)update- Updates an existing stack (should only be used if the stack exists)apply- Idempotently updates or creates a stack, based on whether it exists or notoutputs- Shows the outputs of an existing stackdelete- Deletes an existing stack