Nuts and Bolts

Everyone has opinions and thoughts. Here are some of ours.

Generating Hundreds of CloudFormation Templates with Lono

Posted by Tung Nguyen
on
May 7, 2017

Writing CloudFormation templates becomes difficult to manage once you start using it heavily. Whether it is one complicated template or multiple simple templates, maintaining the templates are a decent amount of work. This post covers a tool I wrote called lono to that manages and generates CloudFormation templates.

How Lono Works

With lono, you compile CloudFormation templates from ERB ruby template. You specify the template variables in the lono template block. An example explains it best. We’ll build an example off of the tutorials from A Simple Introduction to AWS CloudFormation series.

Notice how the new template has an if ERB statement now: <% if @route53 %>. Here’s part of templates/instance.yml:

SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
<% if @route53 %>
HostedZoneName:
Description: The route53 HostedZoneName. For example, "mydomain.com." Don't forget the period at the end.
Type: String
Subdomain:
Description: The subdomain of the dns entry. For example, hello -> hello.mydomain.com, hello is the subdomain.
Type: String
<% end %>

What you are looking at is a lono template view which lono will take with the variables declared in config/templates/base/stacks.rb and combined them to compile them down to a CloudFormation template in the outputs folder. Here’s the config/templates/base/stacks.rb contents:

Note, that lono comes with it’s own lono cfn create command that we could have used, but I used the standard aws cloudformation CLI commands to demonstrate that the generated templates are vanilla CloudFormation templates. We can use these generated templates just as we would use any other CloudFormation template.

Cleanup After Yourself

Remember to clean up and delete the stack so you do not get charged more than you have to.

aws cloudformation delete-stack --stack-name single-instance

Summary

We have taken raw CloudFormation templates and converted it to a lono templates. With this in place, there is a solid foundation to be able to generate multiple CloudFormation templates and allow us to use conditional logic in the ERB templates. As mentioned in the Why Generate CloudFormation Templates with Lono Post, we were able efficiently managing over 250 CloudFormation stacks based on four main core templates with lono. It would have been impossible to manage all of those templates without some type of automation. If you have any questions about lono feel free to reach out. Hopefully, this has been helpful!