Usage

Configset Example

The configset can be written in YAML form. This form is what you’ll typically see in CloudFormation templates out there. Lono adds a little Ruby sprinkles by allowing you to use ERB templating also.

app/configsets/httpd/configset.yml

AWS::CloudFormation::Init:
  config:
    packages:
      yum:
        httpd: []
    files:
      "/var/www/html/index.html":
        content: <%= @html %>
    services:
      sysvinit:
        httpd:
          enabled: true
          ensureRunning: true

This configset will install, configure, and ensure that the httpd server is running, even if the server is rebooted.

The configset can also be written in DSL form. This DSL is provided by Lono. The DSL compiles down to YAML and does the same thing as the YAML form above.

app/configsets/httpd/configset.rb

package("yum",
  httpd: []
)
file("/var/www/html/index.html",
  content: @html
)
service("sysvinit",
  httpd: {
    enabled: true,
    ensureRunning: true,
  }
)

The DSL methods are pretty much a one-to-one mapping to the YAML configset fields. For more methods, see the DSL Docs.

Usage

You can define configsets in the app/configsets. Example:

  • app/configsets/cfn-hup/configset.rb
  • app/configsets/httpd/configset.rb

You configure configsets and tell lono to add them to CloudFormation templates with configs, either in app/blueprints/BLUEPRINT/config/configsets.rb or config/blueprints/BLUEPRINT/configsets.rb. Example:

config/blueprints/ec2/configsets.rb

configset("cfn-hup", resource: "Instance")
configset("httpd", resource: "Instance")

This installs cfn-hup and httpd on the EC2 instance.

More specifically, lono adds the 2 configsets to the CloudFormation template resource with the logical id Instance. The cfn-hup and httpd configsets are added to the Instance.Metadata.AWS::CloudFormation::Init attribute.

You have full control over which configsets to use for each template.

How They Work

Configsets do not magically get applied after being added to the CloudFormation template though. The cfn-init script must be called to apply the configset. Usually the cfn-init script is called in the UserData script. This ensures configsets are applied when instances are launched. Additionally, the cfn-hup script can be set up to apply configsets continuously.

UserData cfn-init

You can make sure configsets are applied when instances are launched by calling the cfn-init script in UserData. Here’s an example UserData script.

#!/bin/bash
yum install -y aws-cfn-bootstrap # install cfn-init
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource Instance --region ${AWS::Region}

The ${AWS::StackName} and ${AWS::Region} will be substituted for their actual values at CloudFormation runtime. The --resource Instance specifies which resource logical id in the CloudFormation template that has the metadata with configset instructions to apply.

Note: On AmazonLinux2 cfn-init is already installed.

Configsets ultimately work with cfn-init and AWS::CloudFormation::Init. The Lono configsets concept empowers you to reuse configsets.