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.

Deploying new version of lambda function

See original GitHub issue

❓ General Issue

The Question

I’m attempting to setup a CodeDeploy deployment group for a Lambda function. The CDK documentation for the Version class states:

If you want to deploy through CloudFormation and use aliases, you need to add a new version (with a new name) to your Lambda every time you want to deploy an update. An alias can then refer to the newly created Version.

This suggests that if I want to make a new CodeDeploy deployment, I should change the version name. For this end, I’m naming the versions using the sha1 of the latest Git commit that affects the Lambda’s code or configuration. However, if I commit code that makes cosmetic changes to the configuration (i.e., to CDK code pertaining to the lambda), that will produce no differences in the CloudFormation template, and I will get the error A version for this Lambda function exists ( 1 ). Modify the function to create a new version.

This suggests that for a new version to be deployed I need to change the code or make semantic changes to the configuration. If this is the case, why require the version name to be unique between versions?

Code:

const lambdaName = basename(__dirname)

async function getRevision(dir: string): Promise<string> {
    const root = basename((await run("git rev-parse --show-toplevel")).line())
    const p = dir.substring(dir.lastIndexOf(root) + root.length + 1, dir.length)
    return (await run(`git rev-list --abbrev-commit -1 HEAD -- ${p}`)).line()
}

export async function myLambda(stack: Stack): Promise<Alias> {
    const f = new Function(stack, lambdaName, {
        runtime: Runtime.GO_1_X,
        handler: "main",
        code: Code.asset(join(__dirname, "lambda.zip")),
    })

    const alias = new Alias(stack, lambdaName + "-alias", {
        aliasName: "live",
        version: f.addVersion(await getRevision(__dirname)),
    })

    new LambdaDeploymentGroup(stack, lambdaName + "-deployment", {
        alias: alias,
        deploymentConfig: LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_10MINUTES,
    })

    return alias
}

Environment

  • **CDK CLI Version: 1.18.0
  • **Module Version: 1.18.0
  • **OS: macOS Catalina
  • **Language: Typescript

Other information

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:25 (16 by maintainers)

github_iconTop GitHub Comments

38reactions
skinny85commented, Dec 10, 2019

Actually, I figured out a workaround 😃 changing the description of the Function is enough to make the Version creation succeed, so this works:

        const func = new lambda.Function(this, 'lambdaName', {
            // whatever properties you need...
            description: `Generated on: ${new Date().toISOString()}`,
        });

        const version = func.addVersion(new Date().toISOString());

        const alias = new lambda.Alias(this, 'lambdaName-alias', {
            aliasName: 'live',
            version: version,
        });

        new codedeploy.LambdaDeploymentGroup(this, 'lambdaName-deployment', {
            alias: alias,
            deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
        });
17reactions
aripalocommented, Feb 29, 2020

Yeah it would be nice if one could do something in the manner of:

const fn = new lambda.VersionedFunction(this, "MyFunc", { /*...*/});

… which would calculate a combined hash of the source code and of the AWS::Lambda::Function.

Then it would also create a new version if the hash has changed.

Not sure if this is doable, but just thinking out loud.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PublishVersion - AWS Lambda
Creates a version from the current code and configuration of a function. Use versions to create a snapshot of your function code and...
Read more >
Working with Lambda function versions - no dogma blog
When you deploy a Lambda function, it is referred to as the “$LATEST” version. If you make a code change and deploy it...
Read more >
Lambda Versioning: Introduction | Knowledge Base - Dashbird
A version of a Lambda function is exactly what the name suggests. Every time new code is deployed or the function configuration needs...
Read more >
Create different versions of an AWS Lambda function
It's awesome that we can edit a lambda function either directly in the AWS Console, or by uploading a new zip file or...
Read more >
Lambda function updating, publishing, versioning question
Hitting save overwrites $LATEST . Once you publish a version, a copy of $LATEST is made, and it's assigned a new numeric version...
Read more >

github_iconTop Related Medium Post

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