question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[lambda]Incorrect Lambda function version calculation causes deployment difficulty

See original GitHub issue

This 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:

https://github.com/aws/aws-cdk/blob/33cc5ec63d648e3d93a51e78d396b6aee16e37ea/packages/%40aws-cdk/aws-lambda/lib/function-hash.ts#L11-L14

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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
jpSimkinscommented, Apr 5, 2022

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 bug feature that we need to deal with…

0reactions
anhtumai-spacehubcommented, Oct 25, 2022

has this error got fixed? and in which version?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot deployment issues in Lambda
Deployment errors prevent the new version from being used and can arise from issues with your deployment package, code, permissions, or tools. When...
Read more >
AWS Lambda Cold Starts: Solving the Problem - Lumigo
In the AWS Lambda console, choose an existing Lambda function. In the Actions drop-down menu, choose the Publish new version option.
Read more >
Problem-solving with lambda | Aapje is Baas
How to go from an idea, to code and deployment on AWS Lambda.
Read more >
AWS error from Python: No module named lambda_function
Error was due to file name of the lambda function. While creating the lambda function it will ask for Lambda function handler.
Read more >
Excel LAMBDA function: how to write and use - Ablebits
If your LAMBDA function results in an error, these troubleshooting tips will help you detect the problem's root cause.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found