Andrew Allison
Andrew Allison

Reputation: 1146

AWS Typescript CDK: A version for this Lambda function exists ( 1 ). Modify the function to create a new version

Here is my CDK code:

    const addVersion = (resourcePrefix: string, lambda: NodejsFunction) => {
      const version = new Version(this, `${resourcePrefix}Version`, {
        lambda,
      });

      const alias = new Alias(this, `${resourcePrefix}VersionAlias`, {
        aliasName: 'current',
        version,
      });

      alias.node.addDependency(version);
    }

The first deployment succeeds, subsequent deployments fail. I'm assuming it's because CDK is attempting to create a new version of the Lambda function each time even when the source code has not been modified. How can I get it to stop doing this?

Upvotes: 3

Views: 3562

Answers (1)

fedonev
fedonev

Reputation: 25809

Reference the Lambda's currentVersion property:

new Alias(this, `${resourcePrefix}VersionAlias`, {
  aliasName: 'current',
  version: lmbda.currentVersion,
});

docs: The fn.currentVersion property can be used to obtain a lambda.Version resource that represents the AWS Lambda function defined in your application. Any change to your function's code or configuration will result in the creation of a new version resource. You can specify options for this version through the currentVersionOptions property.

Upvotes: 1

Related Questions