Lambda Versioning With CDK

I stublied across this hint in my twitter feed, but forgot to book mark it. Thank you to the person that originally posted it.

For a litte while I was trying to figure out how to stop CDK from deleting old lambda version so you could roll back quickly. Turns out it is very simple, you just need to set the rentention policy of the version to retain.

1
2
3
4
5
6
7
8
9
10
11
var function = new Function(this, "example-lambda", new FunctionProps
{
<other set up code>
CurrentVersionOptions = new VersionOptions
{
RemovalPolicy = RemovalPolicy.RETAIN
}
}

var version = function.CurrentVersion;
version.AddAlias("production");

Which gives this in the lamba console

Full code here