Skip to content

Python script & template to run an AWS Cloudformation stack

Notifications You must be signed in to change notification settings

radykal-com/aws-cloudformation-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python script to launch AWS Cloudformation templates

This script allows to create cloudformation stacks easily, just providing the template file and passing required arguments when calling the script.

Note: Running this script may incur in AWS costs not included in free-tier.

Configuration

Software dependencies

In order to run the script the following requirements must be met:

  • Python 3.x (and python binaries path added to system PATH)
  • Python libraries dependencies can be installed with the following command (run from the app root directory): pip install -r requirements.txt

Credentials configuration

Limitations

AWS Region

  • Region is hard-coded in the script, actually set as eu-west-1 so the methods to define it through config files as shown in the Boto3 documentation will not work.

Quick start

There is a cloudformation template included within the script called hello-world.json.

It creates a stack composed of:

  • Auto-scaling group of t2.micro instances (free-tier eligible) from 2 to 6 instances running Windows Server 2012 R2
  • Elastic Load Balancer (public access on port 80)
  • CloudWatch alarms and scaling policies

The stack has some parameters like:

  • IP CIDR to allow RDP connections
  • IP CIDR to access port 8080
  • Email address to notify auto-scaling actions

The script will show you the required parameters if you provide at least the application and environment such as:

python create-stack.py --Application hello-world --Environment development

To add more stacks just put the cloudformation template files inside templates directory with json extension. To call the template just pass the file name of the template as application (without extension).

Useful information

Stack Name

The stack name for each template is automatically set with the following pattern: application-environment so running the hello-world application for development environment will create the stack called hello-world-development

Special parameters

There are some special parameters that act different if present in a template:

  • S3LogsBucketName and S3LogsBucketCreate: These 2 parameters wont be prompt as inputs for the script. The script will automatically pass the name logs-application-environment (logs-hello-world-development for the previous example) to the template and check if the bucket already exists to pass the flag accordly.
  • DefaultVPCId: Automatically filled with the default VPC Id.
  • DefaultRouteTableId: Automatically filled with the Main route-table Id assigned to the Default VPC.
  • DefaultVPCSecurityGroupId: Automaticalled filled with the default security-group Id of the default VPC.

Troubleshooting

Run the script with --Debug true to disable stack rollback on failure.

Error Exit Codes

  • 11: Wrong application-name, the template file could not be found
  • 12: Stack with same name already exists
  • 50: Unexpected behaviour while trying to detect S3 bucket ownership
  • 51: S3 bucket exists with the same name and you have no permissions over it

Predefined tags

The following tags will be created automatically with each stack:

  • application: the application name (template file name)
  • environment: environment (environment called when creating the stack)

Usage example (hello-world)

python create-stack.py --Application hello-world --Environment development --Debug False --InstanceType t2.micro --KeyName aKeyPairName --ScalingNotificationEmail [email protected] --RDPAllowCidrIp 8.8.8.8/24 --PrivateAdminCidrIp 8.8.4.4/32

About

Python script & template to run an AWS Cloudformation stack

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages