CDK Code Pipeline Lambda deployment is deleting old versions with removal policy set to RETAIN
See original GitHub issueAfter following the guide for a Lambda deploying pipeline, I eventually got a running build. I can commit a change to codecommit which will kick off the pipeline. The specified alias will be moved (or created) and point to the new version. But the old version and any alias attached to it will be deleted. I am always left with two versions ($LATEST and the just-deployed one) and one alias (the just-deployed one).
What I expected to happen in the case of a commit that does not change the alias:
- The alias is updated to point to the new version and the old version is not deleted
What I expected to happen in the case of a commit that did change the alias:
- The previous alias(es) points to whatever version they were pointing to before
- The new alias points to the new version and any old versions are not deleted
Reproduction Steps
- Follow this guide
- Change the
FunctionProps.currentVersionOptions.removalPolicy
toRETAIN
. (this actually does not appear to matter) - Change the name of the alias and commit.
- The deployment deletes the previous version and alias and sets the new alias to the new version.
this.lambdaCode = Lambda.Code.fromCfnParameters();
const lambdaFnProps: Lambda.FunctionProps = {
handler: "index.handler",
code: this.lambdaCode,
runtime: Lambda.Runtime.NODEJS_12_X,
description: "description",
role: role,
functionName: this.lambdaFnName,
currentVersionOptions: {
removalPolicy: RemovalPolicy.RETAIN
}
};
const func = new Lambda.Function(this, "LambdaFunction", lambdaFnProps);
const version = func.addVersion(new Date().toISOString());
const alias = new Lambda.Alias(this, 'LambdaAlias', {
aliasName: 'dev2',
version,
});
const deploy = new CodeDeploy.LambdaDeploymentGroup(this, "LambdaDeploymentGroup", {
alias: alias,
deploymentConfig: CodeDeploy.LambdaDeploymentConfig.ALL_AT_ONCE,
});
CDK Diff Output
The output from running cdk diff pretty much shows what ends up happening:
Resources
[-] AWS::Lambda::Version LambdaFunctionVersion20200428T062510953Z1CA2012F destroy
[-] AWS::Lambda::Alias LambdaFunctionVersion20200428T062510953ZAliasdev26C46D216 destroy
[+] AWS::Lambda::Version LambdaFunction/Version2020-04-28T06:39:18.760Z LambdaFunctionVersion20200428T063918760Z149463F3
[+] AWS::Lambda::Alias LambdaFunction/Version2020-04-28T06:39:18.760Z/Aliasdev2 LambdaFunctionVersion20200428T063918760ZAliasdev282DEF3AB
Error Log
Environment
- CDK Version: 1.35
- OS: Mac
- Language : Typescript
Other
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
class Version (construct) · AWS CDK
Specifies a provisioned concurrency configuration for a function's version. removalPolicy? RemovalPolicy, Whether to retain old versions of this function when a ...
Read more >How to set a Deletion Policy on a Resource in AWS CDK
The Deletion Policy from CloudFormation is called Removal Policy in AWS CDK and can be applied to stateful resources to prevent accidental deletion....
Read more >How to remove older versions of Lambda | by Alien - Medium
Currently, there is no way to configure at Lambda level for retaining the number of versions and delete the older versions. A workaround...
Read more >Clean up | AWS CDK Workshop
If you don't want to retain this table, we can set this in CDK code by using RemovalPolicy: Set the DynamoDB table to...
Read more >@aws-cdk/pipelines - npm
If you want or need more control, we recommend you drop down to using the aws-codepipeline construct library directly. This module contains two...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am retaining old versions like this: https://github.com/taimos/cdk-construct-alexa-skill/blob/master/lib/alexa-stack.ts#L71
@skinny85 I did some testing and your suggestions worked. Thanks for the help. Thanks also @hoegertn for the code example.