Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Amazon Announces Extensibility for AWS CloudFormation with AWS Lambda Powered Macros

Amazon Announces Extensibility for AWS CloudFormation with AWS Lambda Powered Macros

Leia em Português

This item in japanese


With AWS CloudFormation developers can model and define their infrastructure as code (IaC). Now Amazon have announced a new feature of AWS CloudFormation called Macros, which allows developers to extend the native syntax of CloudFormation templates by calling out to AWS Lambda Function-powered transformations. 

AWS CloudFormation is an essential building block on the AWS platform, with many services depending upon it. Since its initial release in 2011, CloudFormation has evolved to its current state with the latest addition of Macros. A Macro in AWS CloudFormations has two components: a definition and an implementation. A developer creates a definition starting with the CloudFormation resource type AWS::CloudFormation::Macro, that outlines which Lambda function to use and what the macro should be called. 

Type: "AWS::CloudFormation::Macro"
  Description: String
  FunctionName: String
  LogGroupName: String
  LogRoleARN: String
  Name: String

In the definition, the Name must be unique, and the reference to the Lambda function in the FunctionName needs to be in the same region as the macro. When the developer executes the macro template, it will become available for other templates too. The implementation of the macro is a Lambda function, and it expects a JSON payload.

    "region": "us-east-1",
    "accountId": "$ACCOUNT_ID",
    "fragment": { ... },
    "transformId": "$TRANSFORM_ID",
    "params": { ... },
    "requestId": "$REQUEST_ID",
    "templateParameterValues": { ... }

The fragment in the JSON payload contains either an entire template or relevant portions of the template. Note that the fragment will also be JSON regardless if the template is YAML. Furthermore, the Lambda function returns a JSON response containing:

  • a request ID, the same as one it receives in the request
  • a status field containing success or other values that will fail the ChangeSet
  • and a fragment containing the valid CloudFormation JSON of the transformed template

When developers want to use the macro they can call Fn::Transform with the necessary parameters. 

AWSTemplateFormatVersion: 2010-09-09
    Type: 'AWS::S3::Bucket'
      Name: EchoMacro
        Key: Value

In the case a developer needs to parse a whole template, then they can include it in the list of transforms in the template. When executing this template, the transforms will be collected into a changeset, by calling out to each macro’s specified function and returning the final template according to the blog post.


Randall Hunt, senior technical evangelist at AWS, said in the blog post about the announcement of Macros:

The real power here is that you can extend your existing infrastructure as code with code. The possibilities enabled by this new functionality are virtually unlimited.

There are several reference macros, such as ShortHand, and String Functions, which developers can explore in order to get familiar with macros. The new Macros feature in AWS CloudFormation is available in all AWS regions that provide AWS Lambda. Furthermore, there is no additional CloudFormation charge for Macros, meaning customers are only billed the standard AWS Lambda function charges.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • AWS CloudFormation should use Ruby! That would be a real improvement!

    by Colbert Philippe,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    The macro function features has been long awaited for AWS CloudFormation. CloudFormation lacked abstraction and macro functions will bring some of that. In my opinion AWS should consider dropping the entire pseudo-JSON/YAML format that they are using entirely and adopt a language like Ruby to implement CloudFormation.

    Ruby is syntactically complete, has other syntactic niceties, has class definition, which could be very useful for CloudFormation, and has packaging technology. Yes, bring-in Object-Oriented programming organization in CloudFormation. Ruby has string interpolation like this:

    name = "Ada"
    puts "Hello, #{name}!"

    which is far more elegant that current CloudFormation string building invokation (warning!...following code is very ugly just to build a string).

    - ''
    - - 'arn:'
    - !Ref Partition
    - ':s3:::elasticbeanstalk-*-'
    - !Ref 'AWS::AccountId'

    Ruby would rely on chained and dotted named access rather than indentation level like JSON/YAML do. Combined with the 'let' statement in Ruby, this would go a long way to make CloudFormation clearer to write, read and understand. These features above are very strong assets of Ruby that would enhance CloudFormation considerably.

    Ruby is standard so no need to learn another proprietary language (CloudFormation pseudo-JSON/YAML)! Ruby does not require on statement per line like Yaml and so can make programs that are shorter, more compact. AWS CloudFormation configuration are far too big because of the tendency for one statement per line!

    The advantages of using Ruby over CloudFormation pseudo-JSON/YAML syntax are considerable and self-evident.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p