aws-lambda: calculateLayersHash includes all stack layers instead of only function's layers
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
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_
Contributor guide
Research direction
Start in function-hash.js at calculateLayersHash and inspect how the function's CloudFormation properties expose attached layers when LAMBDA_RECOGNIZE_LAYER_VERSION is enabled. Verify the supplied two-function reproduction, then confirm that adding a layer to Function B no longer changes Function A's hash or causes an unnecessary version update.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100