Skip to content

fix(lambda): exclude unattached stack layers from function version hash - #38012

Open
arieleli01212 wants to merge 3 commits into
aws:mainfrom
arieleli01212:fix/lambda-layer-hash-36713
Open

arieleli01212 wants to merge 3 commits into
aws:mainfrom
arieleli01212:fix/lambda-layer-hash-36713

Conversation

@arieleli01212

Copy link
Copy Markdown

Issue

Fixes #36713

Problem

When the @aws-cdk/aws-lambda:recognizeLayerVersion feature flag is enabled (the default for new CDK projects), calculateFunctionHash appends a layer hash built from fn._layers. The _layers array can contain LayerVersion constructs that exist in the construct tree but were never actually attached to this function's CloudFormation Layers property — for example:

  • A LayerVersion defined in the same stack and used by a different function
  • A layer imported at the stack scope shared across multiple functions

This causes two observable bugs:

  1. Spurious version invalidation — creating any new LayerVersion in a stack forces new Lambda Versions for all existing functions in that stack, even functions whose configuration didn't change. This leads to currentVersion already exists CloudFormation errors during deployment.

  2. 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._layers down to the subset whose resolved ARN appears in the function's own CloudFormation Layers property before passing it to calculateLayersHash. This ensures only layers the function actually uses contribute to its version hash.

const cfnLayerArns: any[] = properties.Layers ?? [];
const attachedLayers = [...fn._layers].filter(layer => {
  const resolvedArn = stack.resolve(layer.layerVersionArn);
  return cfnLayerArns.some((cfnArn: any) => JSON.stringify(cfnArn) === JSON.stringify(resolvedArn));
});
stringifiedConfig = stringifiedConfig + calculateLayersHash(attachedLayers);

Tests Added

  • unattached layer in same stack does not affect function hash — creates a LayerVersion in 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

  • My code follows the style of this project
  • I have tested this manually
  • This PR includes breaking changes

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
@github-actions github-actions Bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels May 27, 2026

@aws-cdk-automation aws-cdk-automation left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@arieleli01212

Copy link
Copy Markdown
Author

Exemption Request: This PR fixes hash calculation logic inside calculateFunctionHash. The correctness of the fix is fully covered by the two new unit tests added to function-hash.test.ts, which directly call calculateFunctionHash and assert the hash is stable when unattached layers exist. An integration test that deploys to AWS would only verify the stack successfully deploys, not whether the hash is correctly scoped to the function's own layers — which is exactly what the unit tests already verify. The previous attempt at this fix (#36818) was closed for staleness before the integ test could be added; the unit tests alone are the appropriate test vehicle here.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label May 27, 2026

This branch was previously deployed

1 inactive deployment
automation — bc370eb3 Deployed Aug 1, 2026 by kumsmrit via validate-pr #358108
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 bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws-lambda: calculateLayersHash includes all stack layers instead of only function's layers

3 participants