7

Lono CloudFormation Framework Introduction Part 6: Lono Seed

 3 years ago
source link: https://blog.boltops.com/2020/02/03/lono-cloudformation-framework-introduction-series-part-6-lono-seed
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-seed-v1.png

Lono CloudFormation Framework Introduction Part 6: Lono Seed

Tung Nguyen Posted by Tung Nguyen on Feb 3, 2020

When working with CloudFormation, you usually configure parameters for the template. The lono seed command helps with this step by generating a starter param file. It evaluates the template itself to generate the starter example values. We’ll use the demo blueprint we created in Lono CloudFormation Framework Introduction Part 1: EC2 Instance.

How It Works

Just run lono seed with the blueprint name.

lono seed BLUEPRINT

Here’s an example with output:

$ lono seed demo
Creating starter config files for demo
Generating CloudFormation templates for blueprint demo:
  output/demo/templates/demo.yml
      create  configs/demo/params/development.txt
$

Let’s look at the contents:

# InstanceType=t3.micro

In this simple demo example, there is only 1 parameter to configure. For templates with several parameters, the lono seed command is a larger time saver.

Notice that the structure also separates configs from blueprints. This allows us to reuse blueprints and follow the DRY principle. For example, the structure allows us to use the same CloudFormation code to create different environments. To generate starter seed files for different environments set LONO_ENV. Example:

$ LONO_ENV=production lono seed demo
Creating starter config files for demo
Generating CloudFormation templates for blueprint demo:
  output/demo/templates/demo.yml
      create  configs/demo/params/production.txt
$

That produces configs/demo/params/production.txt. We can use different parameters for the development vs production environments.

How Starter Values Are Determined

Interestingly, the starter values are derived from the template definition itself. The parameter Description is used. Anything after “Example: “ or “IE: “ is extracted and used as the value. Example:

app/templates/demo.rb:

parameter("Vpc", Description: "Virtual Private Network VPC. Example: vpc-111")

This produces:

configs/demo/params/development.txt

Vpc=vpc-111

Variables Seed Files

For our demo blueprint, lono seed does not create starter configs/demo/variables files. Some blueprints will also generate starter variables config files.

Starter variables cannot be inferred from the CloudFormation template themselves, though. So the blueprint must be authored to support starter variables files. The blueprint tells lono seed how to generate starter variables files with a seeds/configs.rb file. Here’s the interface for the configs.rb:

class Lono::Seed::Configs < Lono::Seed::Base
  def variables
    <<~EOL
      @somevar = "my value"
    EOL
  end
end

The class must be named Lono::Seed::Configs and inherit from Lono::Seed::Base. The variables method returns a String. The String is used for the contents of configs/demo/variables/development.rb. If we add the example above and run lono seed again.

LONO_ENV=development lono seed demo
LONO_ENV=production  lono seed demo

This time it creates the starter variables files also.

$ tree configs/demo/
configs/demo/
├── params
│   ├── development.txt
│   └── production.txt
└── variables
    ├── development.rb
    └── production.rb
$

The contents of configs/demo/variables/development.rb:

@somevar = "my value"

Summary

We covered lono seed in detail in this post. It provides a quick way to generate starter configs for your blueprints. There’s more info on lono seed on the docs. In the next post, we’ll cover variables and loops.

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.

social-boltops-3.gif

Got questions? Check out BoltOps.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK