Easily Manage CloudFormation Templates with lono cfn

Lono is a tool I wrote to generate CloudFormation templates from smarter ERB templates. With it you can use variables and simplify managing your raw CloudFormation templates greatly. I’ve covered using lono to generate templates in these posts:

When using lono the typical process generally looks like this:

1. Update lono CloudFormation templates
2. Run lono generate
3. Construct a CloudFormation parameters file
4. Run aws cloudformation create-stack

With this manual and repetitive process, I forget to run lono generate often. I usually do not realized it until far down the line after the stack has been created with a stale template. I must then delete the stack and start from the top.

Also, constructing the parameters file in CloudFormation’s verbose json array format is a bit bothersome. Building up the long aws cloudformation create-stack command also takes a while. This annoying manual process can be a frustrating developer experience.

lono cfn and lono-params are subcommands in lono that are designed specifically to work with lono to remove this frustrating experience 😊 I will cover them in this post.

Introducing lono param

First, the lono param subcommand is a tool that generates parameter files for the aws cloudformation CLI commands. It does this by reading a env file that simple has key=value pairs. I’ve found that using this simpler format reduces json syntax errors greatly.

For example, given a file at params/asg.txt:

Param1=1
# comments are fine
Param2=2 # comments can go after the line too
Param3=use_previous_value # treated specially

Running lono param generate will create a output/params/asg.json file that contains:

[
{
"ParameterKey": "Param1",
"ParameterValue": "1"
},
{
"ParameterKey": "Param2",
"ParameterValue": "2"
},
{
"ParameterKey": "Param3",
"UsePreviousValue": true
}
]

Introducing lono cfn

The main tool that ties everything together is lono cfn command. It takes the manual steps I’ve mentioned at the beginning of this post:

1. Update lono CloudFormation templates
2. Run lono generate
3. Construct a CloudFormation parameters file
4. Run aws cloudformation create-stack

and simplifies it down to 1 command!

The source code pretty much has these steps described. Using lono-cfn is extremely simple:

$ lono cfn create asg-$(date +%s) --template asg --param asg

Here I’m using the longer form to show you how to specify the template and the params names explicitly. Let’s break down the command:

  • asg-$(date +%s): corresponds to the name of the stack being created
  • --template asg: corresponds to the output/asg.yml file, which is where lono generate writes the CloudFormation template to.
  • --param asg: to the output/params/asg.yml file, which is where lonowrites the parameters file to.

If the template and param name matches the stack name, then you can even get rid of those CLI options and simplify the command further. For example, both of these commands do the exact same thing.

$ lono cfn create asg --template asg --param --asg
$ lono cfn create asg # shorter version

By using lono cfn you will never launch a CloudFormation stack with a stale template because you forgot to run lono generate since lono cfn automatically does it for you. It also automatically updates and builds the parameters files. More details about the tool can be found on the lono cfn README.

Summary

Lono is designed to simply the workflow required to managed CloudFormation templates. With it creating CloudFormation stacks are much easier. Lono templates and parameter env-like files are extremely simple. Lono cfn on top of this all simplifies everything down to a single command. This eliminates errors associated with repeating commands. I hope you enjoy the tools and have found this helpful!

Thanks for reading this far. If you found this post useful, I’d really appreciate it if you recommend this post (by clicking the clap button) so others can find it too! Also, connect with me on LinkedIn.
P.S. Be sure to join the BoltOps newsletter to receive free DevOps tips and updates.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.