Repository navigation
Description
Describe the bug
The calculateLayersHash function in function-hash.js uses lambda._layers to calculate the function's hash. However, _layers contains all
layers registered in the stack if the layer is imported, not just the layers attached to that specific function. This causes unrelated functions' hashes to change when adding layers to other functions.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Library Version
No response
Expected Behavior
calculateLayersHash should only include layers that are actually attached to the specific function, not all layers in the stack.
Current Behavior
The hash calculation includes all layers from lambda._layers, which contains all layers registered in the stack. This means:
- Adding a layer to Function B changes Function A's hash
- Function versions are created even if the function did not change, which leads to a "currentVersion already exists" cloudformation error on deployment, and the stack rolls back
Reproduction Steps
typescript
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
const stack = new cdk.Stack(app, 'TestStack');
// Create two layers
const layer1 = lambda.LayerVersion.fromLayerVersionArn(
stack, 'Layer1',
'arn:aws:lambda:us-east-1:123456789012:layer:my-layer-1:1'
);
const layer2 = lambda.LayerVersion.fromLayerVersionArn(
stack, 'Layer2',
'arn:aws:lambda:us-east-1:123456789012:layer:my-layer-2:1'
);
// Function A with layer1
const functionA = new lambda.Function(stack, 'FunctionA', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.handler',
code: lambda.Code.fromInline('exports.handler = async () => {}'),
layers: [layer1],
});
// Function B with layer2
const functionB = new lambda.Function(stack, 'FunctionB', {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.handler',
code: lambda.Code.fromInline('exports.handler = async () => {}'),
layers: [layer2],
});
// Debug: functionA._layers contains BOTH layer1 and layer2
// This causes functionA's hash to include layer2 even though it's not attached
Possible Solution
No response
Additional Information/Context
The issue is in the calculateLayersHash function in function-hash.js which iterates over
lambda._layers instead of the actual layers attached to the function (which should be retrieved from the CloudFormation properties) with the feature flag LAMBDA_RECOGNIZE_LAYER_VERSION which is enabled by default.
AWS CDK Library version (aws-cdk-lib)
2.234.1
AWS CDK CLI version
2.1101.0
Node.js Version
v24.4.1
OS
Linux
Language
TypeScript
Language Version
No response
Other information
No response
Activity
pahud commentedon Jan 20, 2026
Hi @florianPat,
Thank you for reporting this issue! You've identified a legitimate bug in the
calculateLayersHashfunction. The problem is that when theLAMBDA_RECOGNIZE_LAYER_VERSIONfeature flag is enabled (default), the hash calculation incorrectly usesfn._layerswhich contains all layers registered in the stack for imported layers, rather than just the layers attached to the specific function.This causes the issues you described:
The fix should modify the hash calculation to use only the layers actually attached to the function, likely by extracting them from the CloudFormation properties instead of the internal
_layersarray.This is a great candidate for a community contribution! The fix would involve:
calculateFunctionHashfunction to pass function-specific layersThanks for helping improve the CDK! 🚀
fix(lambda): calculateLayersHash includes only function-specific laye…
fix(lambda): calculateLayersHash includes only function-specific laye…
fix(lambda): calculateLayersHash includes all stack layers aws#36713
fix(lambda): calculateLayersHash includes all stack layers aws#36713
Abhinandansinha01 commentedon Jan 29, 2026
Hi @pahud, I have opened a PR to fix this issue: #36818. This change ensures that we only include relevant layers in the function hash calculation.
fix(lambda): calculate hash only for attached layers
fix(lambda): calculate hash only for attached layers