CloudFormation Macros & Template Transform Authoring Prompt
Design and safely operate CloudFormation macros (Lambda-backed template transforms) to DRY up templates, inject standards, and expand shorthand — with idempotency, error handling, and change-set safety.
- Target user
- Cloud engineers building reusable CloudFormation tooling
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a cloud platform engineer authoring a CloudFormation macro — a Lambda that transforms a template fragment (or whole template) at deploy time — to eliminate boilerplate, enforce org standards (tags, encryption, naming), or expand a terse custom shorthand into full resources. Macros run inside the deploy pipeline, so a buggy one breaks every stack that uses it.
I will provide:
- What the macro should do (global vs. resource-level `Fn::Transform`), and example input/desired output
- The target resource types and the standards to inject
- Constraints (regions, accounts, whether change sets are mandatory)
Your job:
1. **Scope the transform** — decide global macro (in `Transform:` at template top, sees the whole template) vs. snippet macro (`Fn::Transform` on a fragment). Explain the processing order relative to `AWS::Include`, nested stacks, and other macros, and where that ordering bites.
2. **Author the Lambda** — give the handler that reads `event.fragment`, `event.templateParameterValues`, and `event.requestId`, mutates the fragment, and returns `{ requestId, status: "success", fragment }`. Handle the failure path: any exception or `status != success` fails the whole change set. Show defensive parsing so malformed input fails loudly, not silently.
3. **Idempotency & purity** — the transform must be a pure function of its input: no reliance on wall-clock, no external mutation, deterministic output so the same template always yields the same processed template. Flag anything non-deterministic (timestamps, random names) and how to make it stable.
4. **Safety with change sets** — because macros run before the change set is computed, always deploy macro-using stacks via change sets so you can review the *processed* template (`aws cloudformation describe-change-set` / `get-template --template-stage Processed`) before executing. Show those commands.
5. **Versioning & blast radius** — how to version the macro Lambda so a change does not silently alter every dependent stack, and how to test the transform locally against sample fragments before publishing.
Output as: (a) the macro-scope decision with processing-order notes, (b) the Lambda handler with success/failure contract, (c) determinism guarantees and what to remove, (d) the change-set review commands and a versioning/rollout plan. Assume many production stacks will depend on this macro.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
CloudFormation Custom Resources & Lambda-Backed Resources Prompt
Build robust Lambda-backed CloudFormation custom resources that fill provider gaps — with correct CREATE/UPDATE/DELETE semantics, physical IDs, and response signaling that never strands a stack in UPDATE_ROLLBACK_FAILED.
-
AWS CDK Escape Hatches & L1 Override Prompt
Use CDK escape hatches (addPropertyOverride, addOverride, addDeletionOverride, and L2-to-L1 access) to set properties the L2 construct does not expose, without forking constructs or dropping to raw CloudFormation.
-
CloudFormation Dynamic References for SSM & Secrets Manager Prompt
Replace hardcoded secrets and config in CloudFormation templates with resolve dynamic references to SSM Parameter Store and Secrets Manager, including versioning, rotation, and no-echo handling.
-
CloudFormation Change Set Review Prompt
Review a CloudFormation change set before you execute it — classifying every action as add, modify, or replace, flagging the replacements that destroy data, and turning a raw describe-change-set JSON into a go/no-go deployment decision.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.