Some of my favorite lono commands are lono code convert and lono code import. They literally write Ruby code for you. In both cases, the lono code subcommands allow you to take JSON or YAML templates and convert them to the Lono DSL Ruby code.

lono code convert

Here’s the lono code convert example from the docs. The example template.yml has an Instance and SecurityGroup resource.

template.yml:

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 "hello world"
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: demo security group

Here’s an example running convert on the template.yml:

$ lono code convert template.yml > template.rb
INFO: The ruby syntax is valid
INFO: Translated ruby code below:

$ ruby -c template.rb
Syntax OK
$

The INFO messages are written to stderr, and the Ruby code output is written to stdout. We’re using bash redirection write to template.rb. Here’s what template.rb looks like

resource("Instance", "AWS::EC2::Instance",
  InstanceType: ref("InstanceType"),
  ImageId: find_in_map("AmiMap",ref("AWS::Region"),"Ami"),
  SecurityGroupIds: [
    get_att("SecurityGroup.GroupId")
  ],
  UserData: base64("#!/bin/bash\necho \"hello world\"")
)
resource("SecurityGroup", "AWS::EC2::SecurityGroup",
  GroupDescription: "demo security group"
)

The convert command saves a ton of time.

lono code import

The lono code import command works similarly to the convert command. Except it is meant to import a full template and set up the blueprint structure. Example:

$ URL=https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2InstanceWithSecurityGroupSample.template
$ lono code import $URL --blueprint ec2
=> Creating new blueprint called ec2.
      create  blueprints/ec2
      create  blueprints/ec2/ec2.gemspec
      create  blueprints/ec2/.gitignore
      create  blueprints/ec2/CHANGELOG.md
      create  blueprints/ec2/Gemfile
      create  blueprints/ec2/README.md
      create  blueprints/ec2/Rakefile
      create  blueprints/ec2/seed/configs.rb
      create  blueprints/ec2/app/templates
      create  blueprints/ec2/app/templates/ec2.rb
      create  configs/ec2/params/development.txt
      create  configs/ec2/params/production.txt
================================================================
Congrats  You have successfully imported a lono blueprint.

More info: https://lono.cloud/docs/core/blueprints
$

Convert vs Import Summary

The main difference between the convert and import commands:

  • lono code convert: Designed for template snippets. Produces Ruby code meant to be copied and pasted into your current blueprint template.
  • lono code import: Designed to create the full blueprint structure from an existing template.

lono code convert can be used with full templates just fine also. The main difference is that it only produces the Ruby code needed, not the full blueprint structure.

I’ve found myself using the convert command more often. Usually, the workflow is:

  1. Grab a template snippet
  2. Save it to a file like template.yml
  3. Modify the template if needed
  4. Use lono code convert template.yml
  5. Paste the generated Ruby code to my existing blueprint template
  6. Fix the Ruby code if needed
  7. Repeat

The lono code convert and import commands save an incredible amount of time. In this post, we introduced the commands and explained the difference between them.

You might also be interested in using existing templates without having to convert them at all: Use Existing Templates Docs.

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