3

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This did not resolve the issue unfortunately. Is there a certain way I have to create the function? It's important to note I'm using the NodejsFunction construct, not a normal lambda.Function construct.
@AndrewAllison It works fine with NodejsFunction. Make sure to remove the new Version(this, ... and alias.node.addDependency lines. Run cdk diff to verify that your code is not silently causing a new Lambda version to deploy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.