Templates

Templates are what you’ll be working with mostly. Templates belong to a Blueprint and allow you to write CloudFormation templates with the Lono DSL. Templates live in the BLUEPRINT/app/templates folder.

BLUEPRINT
└── app
    └── templates
        └── demo.rb

The starter demo.rb template looks something like this:

app/templates/demo.rb:

description "Demo stack"

parameter("InstanceType", "t3.micro")

mapping("AmiMap",
  "us-east-1": { Ami: "ami-0de53d8956e8dcf80" },
  "us-west-2": { Ami: "ami-061392db613a6357b" }
)

resource("Instance", "AWS::EC2::Instance",
  InstanceType: ref("InstanceType"),
  ImageId: find_in_map("AmiMap", ref("AWS::Region"), "Ami"),
  SecurityGroupIds: [get_att("SecurityGroup.GroupId")],
  UserData: base64(user_data("bootstrap.sh"))
)
resource("SecurityGroup", "AWS::EC2::SecurityGroup",
  GroupDescription: "demo security group",
)

output("Instance")
output("SecurityGroup", get_att("SecurityGroup.GroupId"))

The template gets translated to YAML as part of lono cfn deploy or lono generate. Here’s the example output:

Description: Demo stack
Parameters:
  InstanceType:
    Default: t3.micro
    Type: String
Mappings:
  AmiMap:
    us-east-1:
      Ami: ami-0de53d8956e8dcf80
    us-west-2:
      Ami: ami-061392db613a6357b
Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType:
        Ref: InstanceType
      ImageId:
        Fn::FindInMap:
        - AmiMap
        - Ref: AWS::Region
        - Ami
      SecurityGroupIds:
      - Fn::GetAtt:
        - SecurityGroup
        - GroupId
      UserData:
        Fn::Base64: |-
          !/bin/bash
          echo hi
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: demo security group
Outputs:
  Instance:
    Value:
      Ref: Instance
  SecurityGroup:
    Value:
      Fn::GetAtt:
      - SecurityGroup
      - GroupId

Multiple Templates?

Templates belong to a blueprint. Another way of putting is that each blueprint can have many templates.

While it is possible to create multiple templates for a blueprint, it is generally recommended to create only one template for each blueprint. This allows you to take advantage of the Lono CLI naming conventions and keep your commands short and simple. You may find that it results in more focused blueprint code.

Pro tip: Use the <- and -> arrow keys to move back and forward.

Edit this page

See a typo or an error? You can improve this page. This website is available on GitHub and contributions are encouraged and welcomed. We love pull requests from you!