-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(core): add Template Transform API for post-resolution template modification #36202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lousydropout
wants to merge
6
commits into
aws:main
Choose a base branch
from
lousydropout:feature/hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,409
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…odification Add an opt-in API that allows users to inspect or modify the final CloudFormation template during synthesis, after token resolution but before the template is written to disk. - Add ITemplateTransform interface and TemplateTransforms class - Add App.addTemplateTransform() convenience method - Invoke transforms in Stack._toCloudFormation() after token resolution - Export from core module Transforms can validate templates, inject metadata, modify resources, or fail synthesis by throwing errors. Multiple transforms execute in registration order (FIFO).
Add TemplateTransforms.hasAny() static method that checks if any transforms exist without creating the singleton. Update stack synthesis to skip transform processing entirely when no transforms are registered. Previously, every stack synthesis would create a TemplateTransforms singleton and iterate over its array, even when empty. Now there is zero overhead for users who don't use the feature.
- Add Template Transforms section to aws-cdk-lib README with examples - Simplify JSDoc examples in app.ts to be jsii-compatible (use classes) - Add ITemplateTransform to rosetta default.ts-fixture for example compilation - Add integration test for template transform feature
…shots - Replace simple MetadataInjector example with BlockHardCodedIamArns - Example demonstrates practical security use case: blocking hard-coded ARNs - Uses Annotations.of(stack).addError() for Rosetta/jsii compatibility - Add missing integration test snapshot files
aws-cdk-automation
previously requested changes
Nov 26, 2025
Collaborator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This review is outdated)
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
Update nested stacks test to verify specific stacks are processed with correct nested property values, rather than just checking that some stacks exist with those values.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
beginning-contributor
[Pilot] contributed between 0-2 PRs to the CDK
effort/large
Large work item – several weeks of effort
feature-request
A feature should be added or improved.
p1
pr/needs-further-review
PR requires additional review from our team specialists due to the scope or complexity of changes.
pr/needs-maintainer-review
This PR needs a review from a Core Team Member
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
Partially addresses #17377 by introducing synthesis-time template transforms, fulfilling the request for a way to inspect and modify the final CloudFormation template before it is written. Deploy-time hooks remain out of scope.
Reason for this change
Right now, CDK has ways to customize constructs before synthesis and ways to validate synthesized output through metadata checks. But there is no way to inspect or update the final CloudFormation template right before it’s saved.
This capability enables use cases such as:
Description of changes
This PR introduces Template Transforms, a simple feature that lets users register functions on the
Appthat run right before each stack’s template is written out.New APIs:
ITemplateTransform-- interface withtransformTemplate(stack, template)TemplateTransforms-- stores the transformsApp.addTemplateTransform()-- adds a transform to the appModified APIs:
None. This PR does not modify any existing API.
How it works:
cdk.outaddError) for validation that allows templates to be written but causes the CLI to fail after synthesisThis feature is fully opt-in and non-breaking. If a user does not register any template transforms, no objects or symbols are created unless the user registers a transform and no extra code paths run during synthesis. This is enforced by a
TemplateTransforms.hasAny()check inStack._toCloudFormation(), which completely skips the transform logic unless transforms have been explicitly added. Applications that do not use this API see zero behavioral or performance change.Files changed:
template-transform.ts- new file defining the transform interfaces and registryapp.ts- added theaddTemplateTransform()methodstack.ts- added an opt-in branch to invoke template transforms during synthesisindex.ts- addedexport * from "./template-transform"Describe any new or updated permissions being added
N/A - This feature does not introduce any new IAM permissions.
Description of how you validated changes
22 unit tests checking registration,
hasAny()opt-in behavior, ordering, mutation, errors, and nested stacksIntegration test verifying output changes
Manual testing using a sample CDK app with a local build of
aws-cdk-liblinked into a local CDK CLI:cdk.outyarn buildandyarn rosetta:extract --strict(from withinpackages/aws-cdk-lib) passUsage example
This example demonstrates how template transforms can enforce guardrails during synthesis. The transforms perform:
0.0.0.0/0or::/0, causing the CLI to fail after synthesis."arn:"values instead of token-based references.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.