Digital Age Experts, LLC

CloudFormation Macros

As CloudFormation releases more and more functionality to try to counteract the competition one feature caught our attention here at DAE – CloudFormation (CFN) Macros. CFN Macros gives you the ability to create the equivalent of a built-in function with-in CFN. So now you can create a random number generator function to help with the naming of resources or an explode function to create x number of resources with lookups as part of their custom configuration. CFN Macros provides the capability for much-needed quality of life features that were often envied by the CFN community when they longingly stared at Terraform.

To show how this can improve your everyday CloudFormation usage – let’s look at the Count function.

The Count function (similar in name and use in Terraform) allows you to create n number of a given resources. Say you need to create fifty S3 buckets. In the old days, you would have to define them each within your Cloud Formation, but with Count – you can specify one resource object and then set count = 50. Then Magic.

AWSTemplateFormatVersion: '2010-09-09'
Transform:
  - Count
Resources:
  BucketToCopy:
    Type: AWS::S3::Bucket
    Properties:
BucketName: dae-test-%d
Count: 50

CFN Macros must first be packaged and deployed into the account you plan to use them in (as a Lambda Function).  To make things easier use SAM to deploy your macro and CloudFormation. Once deployed, any CFN in that account can now be used by placing the following in their CFN template.

Transform:
	-<Your_Function_Name>

With Macros now the community can populate CFN with desired functionality on their own, easing the pressure AWS CloudFormation team was facing. It will be exciting to see the AWS Serverless repo be able to host and share macros as well.

By: Gabriel Alix

Close Menu