bazaarvoice / cloudformation-ruby-dsl

Ruby DSL for creating Cloudformation templates
Apache License 2.0
210 stars 76 forks source link

cloudformation-ruby-dsl

A Ruby DSL and helper utilities for building CloudFormation templates dynamically.

Written by Bazaarvoice: see the contributors page and the initial contributions for more details.

Motivation

CloudFormation templates often contain repeated stanzas, information which must be loaded from external sources, and other functionality that would be easier handled as code, instead of configuration.

Consider when a userdata script needs to be added to a CloudFormation template. Traditionally, you would re-write the script by hand in a valid JSON format. Using the DSL, you can specify the file containing the script and generate the correct information at runtime.

:UserData => base64(interpolate(file('userdata.sh')))

Additionally, CloudFormation templates are just massive JSON documents, making general readability and reusability an issue. The DSL allows not only a cleaner format (and comments!), but will also allow the same DSL template to be reused as needed.

Installation

Run gem install cloudformation-ruby-dsl to install system-wide.

To use in a specific project, add gem 'cloudformation-ruby-dsl' to your Gemfile, and then run bundle.

Releasing

See Releasing.

Contributing

See Contributing.

Usage

To convert existing JSON templates to use the DSL, run

cfntemplate-to-ruby [EXISTING_CFN] > [NEW_NAME.rb]

You may need to preface this with bundle exec if you installed via Bundler.

Make the resulting file executable (chmod +x [NEW_NAME.rb]). It can respond to the following subcommands (which are listed if you run without parameters):

Command line options similar to cloudformation commands, but parsed by the dsl.

Any other parameters are passed directly onto cloudformation. (--disable-rollback for instance)

Using the ruby scripts:

template.rb create --stack-name my_stack --parameters "BucketName=bucket-s3-static;SnsQueue=mysnsqueue"

Below are the various functions currently available in the DSL. See the example script for more usage information.

DSL Statements

Add the named object to the appropriate collection.

CloudFormation Function Calls

Invoke an intrinsic CloudFormation function.

Intrinsic conditionals are also supported, with some syntactic sugar.

Reference a CloudFormation pseudo parameter.

Utility Functions

Additional capabilities for file inclusion, etc.

Default Region

The tool defaults to region us-east-1. To change this set either EC2_REGION or AWS_DEFAULT_REGION in your environment.