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.

CDK Code Pipeline Lambda deployment is deleting old versions with removal policy set to RETAIN

See original GitHub issue

After 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

  1. Follow this guide
  2. Change the FunctionProps.currentVersionOptions.removalPolicy to RETAIN. (this actually does not appear to matter)
  3. Change the name of the alias and commit.
  4. 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:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hoegertncommented, Apr 28, 2020
0reactions
zachbarrowcommented, Apr 30, 2020

@skinny85 I did some testing and your suggestions worked. Thanks for the help. Thanks also @hoegertn for the code example.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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