Lono CloudFormation Framework Introduction Part 3: Configs Params Variables
Today we’ll learn about Lono Configs. Lono has 2 types of configs: Params and Variables. Params allow you to affect the templates at run-time. Variables configs allow you to affect the templates at compile-time. We’ll cover both.
Note: Source Code for this tutorial is available at: tongueroo/lono-cloudformation-examples
Structure
First, let’s take a look at the location and structure of blueprints and configs.
blueprints
└── demo
configs
└── demo
├── params
│ ├── development.txt
│ └── production.txt
└── variables
├── development.rb
└── production.rb
Note that the blueprints are in a separate folder from the configs. The configs/demo
folder contains config values for the app/blueprints/demo
blueprint.
Params vs Variables
Here’s a diagram to help explain the difference between Params and Variables.
Params
Params are traditional CloudFormation parameters. They are how most CloudFormation templates are dynamically configured. For example, you can use an InstanceType
parameter to control the instance size to launch: m5.large, m5.small, etc. Parameters can also be combined with more advanced CloudFormation constructs like intrinsic functions and conditions to build programming logic. Here’s an example of Params.
configs/demo/params/development.txt:
InstanceType=t3.small
Lono Params are formatted with env-like values. It’s typically easier to work with than the standard CloudFormation parameters JSON form. It also supports ERB and can reference variables, which we’ll cover next.
Variables
Though you can get pretty far with parameters, sometimes they do not suffice. In this case, you can use variables for more control. Variables allow you to compile different templates entirely. Learning how to use variables enable you to use the full power of the Ruby programming language: if statements, loops, etc. Here’s a simple example of variables.
configs/demo/variables/development.rb:
@desc = "my description for the #{Lono.env} environment"
Example
Continuing with our example from the last tutorial, we’ll use the example configs files from above. The template already uses InstanceType
as a parameter. So we only need to add the @desc
variables to the template. The CloudFormation description section cannot reference parameters, so a variable is required if you wish to configure it dynamically. It should look like this:
blueprints/demo/app/templates/demo.rb
description @desc
We’re ready to deploy the updated example.
$ lono cfn deploy demo
Deploy demo stack...
Generating CloudFormation templates for blueprint demo:
output/demo/templates/demo.yml
Uploading app/files...
Uploading CloudFormation templates...
Uploaded: output/demo/templates/demo.yml to s3://lono-bucket-usp0x9l7fhr4/development/output/demo/templates/demo.yml
Templates uploaded to s3.
Generating parameter files for blueprint demo:
output/demo/params/development.json
Using template: output/demo/templates/demo.yml
Using param: configs/demo/params/development.txt
Generating CloudFormation source code diff...
Running: colordiff /tmp/existing_cfn_template.yml /home/ec2-user/tongueroo/lono-cloudformation-examples/tutorial-3/output/demo/templates/demo.yml
3c3
< Description: Demo stack
---
> Description: my description for the development environment
Parameters passed to cfn.create_change_set:
---
change_set_name: changeset-20190511040909
stack_name: demo
parameters:
- parameter_key: InstanceType
parameter_value: t3.small
template_url: https://lono-bucket-usp0x9l7fhr4.s3.us-west-2.amazonaws.com/development/output/demo/templates/demo.yml
template_body: 'Hidden due to size... View at: output/demo/templates/demo.yml'
Generating CloudFormation Change Set for preview......
CloudFormation preview for 'demo' stack update. Changes:
Modify AWS::EC2::Instance: Instance i-039f6d968eaf48155
Modify AWS::EC2::EIP: IpAddress 44.225.7.25
Are you sure you want to want to update the demo stack with the changes? (y/N)
y
Updating demo stack via change set: changeset-20190511040909
Waiting for stack to complete
04:09:22AM UPDATE_IN_PROGRESS AWS::CloudFormation::Stack demo User Initiated
04:09:26AM UPDATE_IN_PROGRESS AWS::EC2::Instance Instance
04:10:28AM UPDATE_COMPLETE AWS::EC2::Instance Instance
04:10:33AM UPDATE_IN_PROGRESS AWS::EC2::EIP IpAddress
04:10:49AM UPDATE_COMPLETE AWS::EC2::EIP IpAddress
04:10:51AM UPDATE_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack demo
04:10:52AM UPDATE_COMPLETE AWS::CloudFormation::Stack demo
Stack success status: UPDATE_COMPLETE
Time took for stack deployment: 1m 31s.
$
Check the Parameters on the CloudFormation console, to confirm that the Description section is dynamically set.
You can also check the CloudFormation parameters to see that a customized InstanceType=t3.small
was passed.
We’ve confirmed that the InstanceType parameter was passed in and also that the CloudFormation Description was dynamically set.
Cleanup
Let’s destroy the resources so we do not get charged more money than we have to. This is simple.
lono cfn delete demo
Summary
This was a brief introduction to two types of Lono Configs: Params and Variables. We learned how Lono Params are a little easier on the eyes with the env-like format. We also learned a little bit about Lono Variables and how they can be used to compile down different templates. We kept the variable example simple because this is an introduction, in a later post we’ll cover the full power of variables more thoroughly with a loop example. In the next post, we’ll talk about Lono Layering.
Lono Introduction Series
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.