mdarwin
mdarwin

Reputation: 2384

Lambdas on AWS with CloudFormation error:"A version for this Lambda function exists. Modify the function to create a new version."

I'm deploying an AWS lambda using CloudFormation.

In the template I use the flag AutoPublishAlias: v1 which should create a version, add the necessary permissions, and update the alias "v1" to point to the latest version.

When I deploy this change, I get the following error:

A version for this Lambda function exists ( 8 ). Modify the function to create a new version.

I am using CICD. Sometimes I redeploy the same lambda having modified another part of the CloudFormation template (e.g adding the AutoPublishAlias flag).

I thought CloudFormation was supposed to be able to handle this correctly - only updating resources which have changed?

So why am I seeing this error, and is there a workaround other than introducing a dummy code change?

Upvotes: 5

Views: 3887

Answers (2)

Jean-Baptiste Beck
Jean-Baptiste Beck

Reputation: 51

4 years later and same issue...

I found a solution / workaround: Create a unique value, like $(date +%s%N) and put it as a description or if you have multiple functions then set it as an environment variable like the following:

Parameters:
  UniqueValue:
    Type: String
    Description: A unique value generated to avoid version collision
    Default: "0"

Globals:
  Function:
    Environment:
      Variables:
        DEPLOYMENT_ID: !Sub ${UniqueValue}

Upvotes: 0

Pedro
Pedro

Reputation: 489

Please see https://seed.run/docs/serverless-errors/a-version-for-this-lambda-function-exists.html

The error is not Cloudformation, it's Serverless telling CF to deploy a new lambda version, which is in fact the same one.

I saw this issue after upgrading from sls 1 to 2. Had to do dummy changes for every function at once.

After that haven't seen the issue come up.

Upvotes: 1

Related Questions