YAML and AWS CloudFormation: Simplifying Template Creation

AWS CloudFormation

Today AWS announced that CloudFormation will now support YAML. As big fans of YAML, we have been testing this new feature and are not disappointed in the results. Prior to this announcement, JSON specifications were used to write CloudFormation templates. However, we had been writing in YAML and using an in-house YAML CloudFormation generator which helped us avoid the typical pain points associated with JSON. 

Now, we can write CloudFormation templates and create stacks using YAML specifications which helps avoid a step in the process and allows us to use YAML’s clean syntax. The biggest thing we get out of using YAML is not having to keep track of the many JSON braces and commas which as many of you already know, can be a real pain point.

It should be noted that CloudFormation will continue to support JSON. With YAML as a new, additional option.YAML is fully compatible; users can take an existing JSON template and convert it to YAML natively, with no changes. We tried it and it works just as you’d expect.  

Additionally, this announcement has three particularly useful features we are excited about:

  • A new string substitution intrinsic function
    A traditional pain point of CloudFormation was the lack of enough string manipulation functions. Strings had to be constructed using an Fn::Join function which could only concatenate strings. This made it more difficult to create strings on the fly for uses such as naming, tagging, or generating helpful outputs. The intrinsic function Fn::Sub substitutes variables in an input string with their evaluated (collapsed) value. Variables are denoted using ${variable_name} syntax in the string and the values of the variables can be passed in as a mapping, e.g.,

    Name: !Sub
- www.${Domain}.com
- Domain: amazonaws 

  • A shorthand syntax for intrinsic functions
    Intrinsic functions have been a very powerful feature of CloudFormation enabling us to write more complex templates more efficiently, e.g., the Fn::GetAtt function is used to access attributes of other resources within the same CloudFormation template. Intrinsic functions have had a verbose syntax in JSON which often meant typing far too many characters than needed to run a simple intrinsic function. AWS has made it easier already through converting to YAML but have further improved it by creating a specific shorthand format for calling intrinsic functions, e.g. Fn::FindInMap becomes !FindInMap. Note that you can still use the existing intrinsic functions in JSON notation (Fn::FindInMap) in YAML formatted templates. Thus, a native JSON to YAML converter can still enable you to transfer to YAML-based templates.

  • A dot notation for accessing attributes (GetAtt).
    As mentioned above, the intrinsic function Fn::GetAtt returns the value of an attribute from a resource in the template. In the past, the Fn::GetAtt function required its two parameters --the logical name of the resource and the name of the attribute to be retrieved--to be passed in as an array with two elements. In the new release, an attribute can be accessed using a dot notation, very similar to how members of an object can be accessed in object oriented programming languages such as C++, JAVA, etc. E.g.,

 !GetAtt MyLB.DNSName

Read more about AWS CloudFormation by clicking the button below

Read More

We work with several organizations that have embraced YAML and will be telling their stories in an upcoming blog. Also, be on the lookout for an article with instructions on how to convert existing JSON CloudFormation templates into YAML. 

If you are seeking additional expertise when it comes to cloud architecture planning, give us a call today. Our certified consultants are expert at AWS design that maps business needs to DevOps-based architecture. For additional reading on using CloudFormation in a DevOps architecture, please check out blog post on this topic.

 

Did you find this useful?  

Interested in getting tips, best practices and commentary delivered regularly? Click the button below to sign up for our blog and set your topic and frequency preferences.

Sign Me Up!

About the Author

Flux7 Labs
Find me on:

Join Us

Join thousands of technology enthusiasts, subscribe and get expert perspective in your inbox.

Connect With Us