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.

app/templates/demo.rb

@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("Instance#{n}")
  output("SecurityGroup#{n}", get_att("SecurityGroup#{n}.GroupId"))
end

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
    <<~EOL
      @instance_count = 1
    EOL
  end
end

Configure and Deploy

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

lono seed demo

configs/demo/variables/development.rb:

@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:

Summary

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