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:
- Created 4 years ago
- Reactions:12
- Comments:25 (16 by maintainers)
Top GitHub Comments
Actually, I figured out a workaround 😃 changing the
description
of the Function is enough to make the Version creation succeed, so this works:Yeah it would be nice if one could do something in the manner of:
… 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.