Lono CloudFormation Framework Introduction Part 7: Variables and Loops

 1 year ago
source link: https://blog.boltops.com/2020/02/11/lono-cloudformation-framework-introduction-series-part-7-variables-and-loops
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Lono CloudFormation Framework Introduction Part 7: Variables and Loops

Tung Nguyen Posted by Tung Nguyen on Feb 11, 2020

The Lono DSL is powerful because it allows you to use the Ruby programming language to generate CloudFormation templates. You can use conditional logic constructs that you’re used to: loops, variables, if statements, methods, etc. However, the Lono DSL tries not to impose itself and take over CloudFormation’s declarative nature. Instead, Lono’s approach is to add programming “sprinkles” on top of CloudFormation. Ultimately, you get the best of both worlds.

Loop Example

Continuing with our demo example and what we’ve learned from previous posts, we’ll create a simple loop to show the power of the Lono DSL. The loop creates multiple EC2 instances instead of just one.

Let’s add a variable call @instance_count and update the template to use it.


@instance_count ||= 1 # default to 1
@instance_count.times do |i|
  n = i + 1
  resource("Instance#{n}", "AWS::EC2::Instance",
    InstanceType: ref("InstanceType"),
    ImageId: find_in_map("AmiMap", ref("AWS::Region"), "Ami"),
    SecurityGroupIds: [get_att("SecurityGroup#{n}.GroupId")],
    UserData: base64(sub(user_data("bootstrap.sh")))
  resource("SecurityGroup#{n}", "AWS::EC2::SecurityGroup",
    GroupDescription: "demo security group",
  output("SecurityGroup#{n}", get_att("SecurityGroup#{n}.GroupId"))

The @instance_count variable controls the number of instances and security groups that will be created.

Lono seed/configs.rb

Let’s also create a seed/configs.rb file to help users using the blueprint set up starter configs.

class Lono::Seed::Configs < Lono::Seed::Base
  def variables
      @instance_count = 1

Configure and Deploy

We can now use lono seed to configure and deploy the blueprint.

lono seed demo


@instance_count = 1

Let’s adjust the variables config to use @instance_count = 2. You can see how variables affect the template with lono summary.

$ lono summary demo
=> CloudFormation Template Summary for template demo:
# Parameters Total (1)
# InstanceType=t3.micro #
# Resources:
  2 AWS::EC2::Instance
  2 AWS::EC2::SecurityGroup
  4 Total

Let’s go ahead and deploy:

lono cfn deploy demo

Check the CloudFormation console, and we’ll see something like this:


And the ec2 console:



In this post, we went through simple variables and loop example. It’s just the tip of the iceberg in terms of power with the Lono DSL. The rest of the iceburg is the full Ruby programming language.

Matz created Ruby to make programmers happy. The Lono DSL is built on top of Ruby. The more I’ve developed Lono and used it, the more I’ve come to appreciated Matz, Ruby’s power, and it’s beauty.

Prebuilt CloudFormation Templates

You may be interested in BoltOps Pro blueprints. Blueprints are essentially CloudFormation templates packaged up in a convenient and reusable way. The BoltOps Pro subscription gives you access to all the blueprints in the BoltOps Pro GitHub organization. Just configure, deploy, and run.

Also, if there is a blueprint you would like to see in the future. Please feel free to send us your suggestions: Pro Blueprint Suggestion.

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.

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK