Repository navigation
fix(lambda): exclude unattached stack layers from function version hash - #38012
arieleli01212 wants to merge 3 commits into
Conversation
When `LAMBDA_RECOGNIZE_LAYER_VERSION` is enabled (default for new projects), `calculateFunctionHash` builds the layer portion of the hash by iterating `fn._layers`. That array can contain `LayerVersion` constructs that live in the construct tree but were never actually attached to this function's CloudFormation `Layers` property (e.g. a layer defined in the same stack that belongs to a different function, or an unattached layer used only for reference). This causes two observable bugs: 1. Creating a new `LayerVersion` in the same stack changes the hash of every existing function, forcing new Lambda Versions to be created even though those functions are unchanged. 2. Adding a layer to Function B can change the hash of Function A if they share layers imported at the stack scope. Fix: filter `fn._layers` to the subset whose resolved ARN appears in the function's own CloudFormation `Layers` property before passing the list to `calculateLayersHash`. This mirrors what the CloudFormation template actually captures and avoids false hash invalidations. Tests added for: - unattached `LayerVersion` in the same stack not affecting function hash - imported layer added to Function B not changing Function A's hash Fixes aws#36713
There was a problem hiding this comment.
The pull request linter fails with the following errors:
❌ Fixes must contain a change to an integration test file and the resulting snapshot.
If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.
✅ A exemption request has been requested. Please wait for a maintainer's review.
|
Exemption Request: This PR fixes hash calculation logic inside |
Issue
Fixes #36713
Problem
When the
@aws-cdk/aws-lambda:recognizeLayerVersionfeature flag is enabled (the default for new CDK projects),calculateFunctionHashappends a layer hash built fromfn._layers. The_layersarray can containLayerVersionconstructs that exist in the construct tree but were never actually attached to this function's CloudFormationLayersproperty — for example:LayerVersiondefined in the same stack and used by a different functionThis causes two observable bugs:
Spurious version invalidation — creating any new
LayerVersionin a stack forces new Lambda Versions for all existing functions in that stack, even functions whose configuration didn't change. This leads tocurrentVersion already existsCloudFormation errors during deployment.Cross-function hash contamination — adding a layer to Function B changes the hash of Function A if both functions share a layer imported at the stack scope.
Fix
Filter
fn._layersdown to the subset whose resolved ARN appears in the function's own CloudFormationLayersproperty before passing it tocalculateLayersHash. This ensures only layers the function actually uses contribute to its version hash.Tests Added
unattached layer in same stack does not affect function hash— creates aLayerVersionin the same stack without attaching it to the function and asserts the hash is unchanged.imported layer added to one function does not affect hash of another function in the same stack— verifies that adding an imported layer to Function B doesn't change Function A's hash.Checklist