[lambda]Incorrect Lambda function version calculation causes deployment difficulty
See original GitHub issueThis is 🐛 Bug Report.
It is possible to attempt get an error from CloudFormation when making a legitimate change to a Lambda function in a CDK stack.
Reproduction Steps
Here is an example stack:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
export class SampleStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new lambda.Function(this, 'Function', {
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
code: lambda.InlineCode.fromInline(`exports.handler = (event) => "hello world"`),
// reservedConcurrentExecutions: 10 // after creating stack, uncomment this line and re-deploy
}).currentVersion.addAlias('live');
}
}
What did you expect to happen?
When I uncomment the reservedConcurrentExecutions: 10
property, I expect cdk deploy
to update the Lambda function’s reserved concurrency configuration.
What actually happened?
I get the following error from the CloudFormation service:
% npx cdk deploy
SampleStack: deploying...
SampleStack: creating CloudFormation changeset...
3:59:04 PM | CREATE_FAILED | AWS::Lambda::Version | FunctionCurrentVer...faacc45be2b3a1451e
A version for this Lambda function exists ( 1 ). Modify the function to create a new version.
Environment
- CDK CLI Version:
1.74.0 (build e86602f)
- Node.js Version: v15.0.1
- OS : MacOS
- Language (Version): all
Suspected cause
I suspect the responsible code is these lines:
The issue is that the string generated by JSON.stringify(config)
on line 14 differs like this between the two iterations of the stack (prettified):
--- before 2020-11-18 16:17:45.000000000 +1100
+++ after 2020-11-18 16:17:37.000000000 +1100
@@ -1,26 +1,27 @@
{
"Resources": {
"Function76856677": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = (event) => \"hello world\""
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"FunctionServiceRole675BB04A",
"Arn"
]
},
- "Runtime": "nodejs12.x"
+ "Runtime": "nodejs12.x",
+ "ReservedConcurrentExecutions": 10
},
"DependsOn": [
"FunctionServiceRole675BB04A"
],
"Metadata": {
"aws:cdk:path": "SampleStack/Function/Resource"
}
}
}
}
\ No newline at end of file
This causes the AWS::Lambda::Version
resource to have a different logical ID:
--- before 2020-11-18 16:15:54.000000000 +1100
+++ after 2020-11-18 16:16:07.000000000 +1100
@@ -40,49 +40,50 @@
"Code": {
"ZipFile": "exports.handler = (event) => \"hello world\""
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"FunctionServiceRole675BB04A",
"Arn"
]
},
- "Runtime": "nodejs12.x"
+ "Runtime": "nodejs12.x",
+ "ReservedConcurrentExecutions": 10
},
"DependsOn": [
"FunctionServiceRole675BB04A"
],
"Metadata": {
"aws:cdk:path": "SampleStack/Function/Resource"
}
},
- "FunctionCurrentVersion4E2B226126dc1a973a7e75ab5d120224c6c45fb3": {
+ "FunctionCurrentVersion4E2B2261de715023e30143628ce2452cd16d155d": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "Function76856677"
}
},
"Metadata": {
"aws:cdk:path": "SampleStack/Function/CurrentVersion/Resource"
}
},
"FunctionCurrentVersionAliaslive2D187DC4": {
"Type": "AWS::Lambda::Alias",
"Properties": {
"FunctionName": {
"Ref": "Function76856677"
},
"FunctionVersion": {
"Fn::GetAtt": [
- "FunctionCurrentVersion4E2B226126dc1a973a7e75ab5d120224c6c45fb3",
+ "FunctionCurrentVersion4E2B2261de715023e30143628ce2452cd16d155d",
"Version"
]
},
"Name": "live"
},
"Metadata": {
"aws:cdk:path": "SampleStack/Function/CurrentVersion/Aliaslive/Resource"
}
},
"CDKMetadata": {
Which in turn causes CloudFormation to return that error. This problem occurs whenever the CloudFormation function resource synthesised by CDK differs in any fields that the Lambda service does not include in its versioning. Such fields include e.g. Properties.ReservedConcurrentExecutions
, Properties.Tags
, and DependsOn
. We should instead calculate a hash of only the versionable properties of a function.
Let me know if you need any more details or anything else from me!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:9 (2 by maintainers)
Top GitHub Comments
Issue still happens with the cdk v2 update from v1. Found it easier to delete the lambda function and recreate it than trying to implement the different hacks the CDK team has suggested in other threads. It would be nice to allow for a force new version flag as this seems more like a proper fix than to change the description (not ideal at all), or create and alias (hacky at best). There doesn’t really seem to be a proper fix for this and it’s a
bugfeature that we need to deal with…has this error got fixed? and in which version?