Anyone using Amazon Web Services will hopefully start using the CloudFormation service early on to start templating and automating the creation of their infrastructure. Until recently, these templates had to be written using the .json file format – however, this week (Sept. 2016), AWS announced that the templates could now be written in .yaml.

Our project

We’ve been working on a project to move a website away from a monolithic app to a more loosely coupled set of services. As part of this, I created a CloudFormation template that created a SQS message queue, gave the queue permission to accept messages from S3 buckets and then created a S3 bucket which was configured to send notifications to the SQS when an upload occurs.

Moving cloudformation from json to yaml

I’d initially written the CloudFormation template in json with a fairly standard layout including parameters, resources and some outputs. For example, the parameter section looked like this:

But as I was finishing testing, the announcement about using yaml was made and I translated the json to yaml as below:

Each of the sections above creates two parameters which are used to pass parameters input when running the template to customise resource names.

The resource section had a number of entries such as

which creates the S3 bucket and gives it permission to send events to the SQS created earlier.

Rewriting this in YAML, leads to

Personally (and I understand that this probably comes down to formatting you’re used to working with), the yaml looks much cleaner to me – the lack of braces and quotes means the template is much easier to follow (at least to me 🙂).

The big winner for me though is that the yaml syntax allows for comments. There have been several posts in the AWS forums asking for some method of allowing comments; the only suggestions until now were to use the ‘clunky‘ metadata sections to pass information – in yaml, the top of my resources section looks like:

If you’d like to see the full versions of the templates, click on the links to see the json version or the yaml one.

Things to watch out for.

If you’re not used to yaml, you do need to be careful with the indentation – I’d suggest if you have a json template already and are having problems, look at one of the online json->yaml converters (for example, http://www.json2yaml.com/)