BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

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

Bookmarks

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"
Properties:
  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
Resources:
  MyS3Bucket:
    Type: 'AWS::S3::Bucket'
    Fn::Transform:
      Name: EchoMacro
      Parameters:
        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.

 
Source: https://aws.amazon.com/blogs/aws/cloudformation-macros/

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

Adoption
Style

BT