Lono CloudFormation Framework Introduction Part 5: Lono Code Convert and Import
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:
- Grab a template snippet
- Save it to a file like
template.yml
- Modify the template if needed
- Use
lono code convert template.yml
- Paste the generated Ruby code to my existing blueprint template
- Fix the Ruby code if needed
- 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.
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.
Got questions? Check out BoltOps.
You might also like
More tools:
-
Kubes
Kubes: Kubernetes Deployment Tool
Kubes is a Kubernetes Deployment Tool. It builds the docker image, creates the Kubernetes YAML, and runs kubectl apply. It automates the deployment process and saves you precious finger-typing energy.
-
Jets
Jets: The Ruby Serverless Framework
Ruby on Jets allows you to create and deploy serverless services with ease, and to seamlessly glue AWS services together with the most beautiful dynamic language: Ruby. It includes everything you need to build an API and deploy it to AWS Lambda. Jets leverages the power of Ruby to make serverless joyful for everyone.
-
Lono
Lono: The CloudFormation Framework
Building infrastructure-as-code is challenging. Lono makes it much easier and fun. It includes everything you need to manage and deploy infrastructure-as-code.