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(:instance_type, "t3.micro")

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

resource(:instance, "AWS::EC2::Instance",
  instance_type: ref(:instance_type),
  image_id: find_in_map(:ami_map, ref("AWS::Region"), :ami),
  security_group_ids: [get_att("security_group.group_id")],
  user_data: base64(user_data("bootstrap.sh"))
)
resource(:security_group, "AWS::EC2::SecurityGroup",
  group_description: "demo security group",
)

output(:instance)
output(:security_group, get_att("security_group.group_id"))

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 recommended to create only 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!