feat: Add CloudFormation Language Extensions support (Fn::ForEach)#8637
Open
feat: Add CloudFormation Language Extensions support (Fn::ForEach)#8637
Conversation
Add core library for processing CFN Language Extensions including: - Fn::ForEach processor for iterating over collections - Fn::Length, Fn::ToJsonString intrinsic function resolvers - Fn::FindInMap with DefaultValue support - Conditional DeletionPolicy/UpdateReplacePolicy support - SAM CLI integration module with partial resolution mode Includes comprehensive unit tests and property-based tests. Addresses GitHub issue #5647
- Add in-memory template expansion with original template preservation - Add Stack.original_template_dict for CloudFormation deployment - Add PACKAGEABLE_RESOURCE_ARTIFACT_PROPERTIES and DynamicArtifactProperty - Add _process_language_extensions() to SamTranslatorWrapper - Add ForEach guards to validator and warning system - Support locally resolvable collections with clear error messages Templates are expanded in memory for local operations while preserving the original Fn::ForEach structure for CloudFormation deployment.
- Expand Fn::ForEach in memory to build all generated functions
- Preserve original template structure in .aws-sam/build/template.yaml
- Generate Mappings for dynamic artifact properties (CodeUri: ./${Name})
- Replace dynamic CodeUri with Fn::FindInMap referencing generated Mappings
- Support --resource-id with expanded function names
- Preserve Fn::ForEach structure in packaged template - Generate Mappings with S3 URIs for dynamic artifact properties - Use content-based S3 hashing for each expanded artifact - Emit warning for parameter-based collections with dynamic CodeUri - Validate collection values for CloudFormation Mapping key compatibility
- Upload original unexpanded template to CloudFormation - Add clear error message for missing Mapping keys at deploy time - Suggest re-running sam package when collection values change
- Test sam build with static and dynamic CodeUri - Test sam package preserves Fn::ForEach with Mappings transformation - Test sam validate with valid/invalid language extension templates - Test sam local invoke with expanded function names - Test sam local start-api with ForEach-generated API endpoints - Test nested stacks with language extensions
0be94d0 to
5d6cbf3
Compare
Contributor
Author
|
Integration test: https://github.com/aws/aws-sam-cli/actions/runs/21808626015 |
The integration tests incorrectly expected "${Name}" and "${FunctionName}"
as the second argument to Fn::FindInMap, but the implementation correctly
uses {"Ref": "Name"} and {"Ref": "FunctionName"} because bare ${Var}
strings are not resolved by Fn::ForEach inside Fn::FindInMap arguments.
- Add _validate_foreach_nesting_depth() and _calculate_max_foreach_depth() methods to ForEachProcessor to validate nesting depth before expansion - Define MAX_FOREACH_NESTING_DEPTH = 5 constant per CloudFormation limits - Integrate validation into process_template() to fail early with clear error - Add unit tests for depth calculation and validation (14 new tests) - Add integration tests for sam validate and sam build with nested loops - Create test templates for valid (5 levels) and invalid (6 levels) nesting - Fix mypy type annotation for empty dict in test file Validates Requirements: 18.1-18.7
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR adds support for CloudFormation Language Extensions in SAM CLI, addressing GitHub issue #5647.
Features
Fn::Ifin resource policiesKey Design Decisions
Fn::ForEachblocks with dynamic artifact properties (e.g.,CodeUri: ./src/${Name}) are supported via a Mappings transformationFn::ForEachcollections must be resolvable locally; cloud-dependent values (Fn::GetAtt,Fn::ImportValue) are not supported with clear error messagesSupported Commands
sam build- Builds all expanded functions, preserves original templatesam package- PreservesFn::ForEachstructure with S3 URIssam deploy- Uploads original template for CloudFormation to processsam validate- Validates language extension syntaxsam local invoke- Invokes expanded functions by namesam local start-api- Serves ForEach-generated API endpointssam local start-lambda- Serves all expanded functionsExample
Resolves #5647
Testing
Checklist