Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 866
Andrew Allison Avatar asked Aug 05 '26 04:08

Andrew Allison


1 Answers

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.

like image 147
fedonev Avatar answered Aug 08 '26 11:08

fedonev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!