This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Small script to manipulate AWS CloudFormation stacks

Project Description

A Python script to generate and manage AWS CloudFormation stacks based on YAML inputs.

Define stacks and their templates in YAML and make use of Jinja2 in the template definition to generate a finished JSON CloudFormation template.

Stacks

A stack defines the static values and dynamic parameters that are put into the template to generate a usable CloudFormation template.

You can define multiple stacks that use the same template by using a different region or environment.

In the examples below I use a single template to define a CloudFormation stack for the regions eu-west-1 and eu-central-1. The example creates one subnet per availability zone, something that differs between the two regions. So, instead of limiting ourself to just using two availability zones, we use a bit of Jinja2 templating and simply generate the subnets from a list.

Templates

A template is a YAML file that is pre-processed by Jinja2 before being turned into JSON. It gets values from the stack and is then generated to provide a finished stack that can be used by CloudFormation.

Create your first stack and template

First, create two directories, stacks and templates. These folders will contain your stacks and your templates.

Then, create two stacks, one for region eu-west-1 and one for eu-central-1. You can copy paste the code below.

# stacks/example.eu-west-1.yml
---
name: Example
values:
  azs:
    - eu-west-1a
    - eu-west-1b
    - eu-west-1c
# stacks/example.eu-central-1.yml
---
name: Example
values:
  azs:
    - eu-central-1a
    - eu-central-1b

Next, create a template that looks like this:

# templates/example.yml
---
AWSTemplateFormatVersion: "2010-09-09"

Description: Example stack template

Resources:
{% for az in azs %}
  {{ "Subnet%s:" | format(az[-1].upper()) }}
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: {{ az }}
      # Other properties are not present, because the example
      # would become too long...
{% endfor %}

That’s it! Now it’s time to generate the CloudFormation files. Run cfn --region eu-west-1 generate example and cfn --region eu-central-1 generate example. You should have a new folder output which contains two files, example.eu-west-1.json and example.eu-central-1.json. These are the ready to use CloudFormation templates.

The generated CloudFormation stack template for stacks/example.eu-west-1.yml would look something like this:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Example stack template",
    "Resources": {
        "SubnetA": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "eu-west-1a"
            }
        },
        "SubnetB": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "eu-west-1b"
            }
        },
        "SubnetC": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "eu-west-1c"
            }
        }
    }
}

You can validate these files with the aws-cli tool by running aws cloudformation validate-template --template-body file://output/example.eu-central-1.json.

To create the stack using the cfn tool, run cfn --region eu-west-1 create example.

If you’ve made changes to the stack and wish to update, run cfn --region eu-west-1 update example.

Run cfn --region eu-west-1 delete example to delete an existing stack created by the cfn tool.

Release History

Release History

This version
History Node

0.0.11

History Node

0.0.10

History Node

0.0.9

History Node

0.0.7

History Node

0.0.6

History Node

0.0.5

History Node

0.0.4

History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
cfn-0.0.11.tar.gz (4.8 kB) Copy SHA256 Checksum SHA256 Source Sep 27, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting