Easily Manage CloudFormation Templates - Introduction to Lono Template Generation
Lono is a tool that generates CloudFormation templates from smart 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:
- Why Generate CloudFormation Templates with Lono
- Generating Hundreds of CloudFormation Templates with Lono
- AutoScaling CloudFormation Template with Lono
When using lono the typical process looks like this:
- Update lono CloudFormation templates
- Run lono generate
- Construct a CloudFormation parameters file
- Run aws cloudformation create-stack
This is a manual and repetitive process which is subject to plenty of human errors along the way. I would forget step 2 and not run lono generate on accident. I would only realize my silly mistake after the stack errors and rolls back. For step 3, constructing the parameters file with CloudFormation’s verbose JSON array format is painstaking. For step 4, building up the long aws cloudformation create-stack
command eats more precious time. This annoying manual process is a frustrating developer experience.
Lono also has param generation and CloudFormation lifecycle commands to remove this frustrating experience 😊 The official documentation for lono is at http://lono.cloud. I will give an introduction these lono commands in this post.
Review lono generate
First, you should read through the Generating Hundreds of CloudFormation Templates with Lono post to understand how lono generates CloudFormation templates. The generation phase is the first phase that lono automates. That post covers in detail how lono generates templates. For your convenience here is the diagram that shows the entire process including the generation process:
Introducing lono param
Next, the lono param
subcommand is a tool that generates parameter files for the aws cloudformation CLI commands. It does this by reading an env file that simply has key=value pairs format. Using this simple format reduces JSON syntax errors.
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/stag/asg.json file that contains:
[
{
"ParameterKey": "Param1",
"ParameterValue": "1"
},
{
"ParameterKey": "Param2",
"ParameterValue": "2"
},
{
"ParameterKey": "Param3",
"UsePreviousValue": true
}
]
Introducing lono cfn
The main command that ties everything together is lono cfn
. It takes the manual steps I’ve mentioned at the beginning of this post:
- Update lono CloudFormation templates
- Run lono generate
- Construct a CloudFormation parameters file
- Run aws cloudformation create-stack
and simplifies it down to a single 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 and explicitly specifying the template and the param options. 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/stag/asg.yml
file, which is where lono writes 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 generated template since lono cfn
automatically calls lono generate
. It also automatically builds the parameters files. More details about the tool can be found on the official documentation site: http://lono.cloud.
Summary
Lono is designed to simplify 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. The command lono cfn
on top of this simplifies everything down to a single command. This eliminates manual and human error. I hope you enjoy this post and found find lono helpful!
Thanks for reading this far. If you found this article useful, I'd really appreciate it if you share this article so others can find it too! Thanks 😁 Also follow me on Twitter.
Got questions? Check out BoltOps.
You might also like
More tools:
-
Kubes
Kubes: Kubernetes Deployment Tool
Kubes is a Kubernetes Deployment Tool. It builds the docker image, creates the Kubernetes YAML, and runs kubectl apply. It automates the deployment process and saves you precious finger-typing energy.
-
Jets
Jets: The Ruby Serverless Framework
Ruby on Jets allows you to create and deploy serverless services with ease, and to seamlessly glue AWS services together with the most beautiful dynamic language: Ruby. It includes everything you need to build an API and deploy it to AWS Lambda. Jets leverages the power of Ruby to make serverless joyful for everyone.
-
Lono
Lono: The CloudFormation Framework
Building infrastructure-as-code is challenging. Lono makes it much easier and fun. It includes everything you need to manage and deploy infrastructure-as-code.