How and Why to use CloudFormation Macros

How and Why to use CloudFormation Macros


Related discussions:

So this is somewhat similar to Terraform?

I still don’t understand why macros were created. If you need meta programming in your templates, you should be using a proper programming language to generate them, not a deployed Lambda function. Take all that code you were going to put in a Lambda function, and instead use it when your template is generated.

Macros feel like a Rube Goldberg machine of complexity.

Sorry if I sound negative. I know I’m not forced to use this feature, and I don’t use it. I guess I want to know if this feature would be at all useful because I can’t see how.

I totally agree.

I include a CloudFormation template compile phase from whatever language to JSON as part of CI.

I can’t think of many use cases for macros that can’t be solved in a cleaner way by pure functions in my language of choice.

Wish they’d just work on getting CDK production ready. This feels like a hack

Because in many cases using CloudFormation with a few macros is good enough to do what needs to be done.

Adding a layer on top of CloudFormation is no silver bullet. It is one more language or framework to learn and one more layer of complexity to debug when don’t work as planned.

It really depends what you’re building and how flexible and complicated your templates need to be.

I don’t see how adding a language on top during deployment time is any harder than adding a language via a deployed Lambda function. A macro is also another language and framework to be learned (Lambda plus whatever language you use in your Lambda), and is definitely more complex to debug than a local script.

I’m not saying jump to troposphere or some other DSL and rewrite all of your templates. I’m saying you can write a script to transform your raw JSON much easier than writing/deploying a Lambda function.

The complexity of adding a Lambda function is much higher than wrapping your CloudFormation templates in a script. That is, unless I’m missing some much more powerful use case of macros (which from the many times I’ve asked this question it doesn’t seem I am missing anything).

The AWS SAM resources are implemented this way I believe. The macro takes the SAM resources and transforms it into a broader set of resources.