Skip to content

Commit

Permalink
feat(lambda): add a newVersion method. (#2099)
Browse files Browse the repository at this point in the history
This method is useful when deploying a Lambda through a CodePipeline.
It creates a new Version every time the CDK code is executed,
that way making sure an updated Lambda Alias will not point to an old Version.
  • Loading branch information
skinny85 authored Mar 29, 2019
1 parent b314ecf commit 6fc179a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,25 @@ export class Function extends FunctionBase {
});
}

/**
* Add a new version for this Lambda, always with a different name.
*
* This is similar to the {@link addVersion} method,
* but useful when deploying this Lambda through CodePipeline with blue/green deployments.
* When using {@link addVersion},
* your Alias will not be updated until you change the name passed to {@link addVersion} in your CDK code.
* When deploying through a Pipeline,
* that might lead to a situation where a change to your Lambda application code will never be activated,
* even though it traveled through the entire Pipeline,
* because the Alias is still pointing to an old Version.
* This method creates a new, unique Version every time the CDK code is executed,
* and so prevents that from happening.
*/
public newVersion(): Version {
const now = new Date();
return this.addVersion(now.toISOString());
}

private renderEnvironment() {
if (!this.environment || Object.keys(this.environment).length === 0) {
return undefined;
Expand Down
30 changes: 28 additions & 2 deletions packages/@aws-cdk/aws-lambda/test/test.alias.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { beASupersetOfTemplate, expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import cloudwatch = require('@aws-cdk/aws-cloudwatch');

import { beASupersetOfTemplate, expect, haveResource } from '@aws-cdk/assert';
import { Stack } from '@aws-cdk/cdk';
import { Test } from 'nodeunit';
import lambda = require('../lib');
Expand Down Expand Up @@ -41,6 +40,33 @@ export = {
test.done();
},

'can use newVersion to create a new Version'(test: Test) {
const stack = new Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NodeJS610,
});

const version = fn.newVersion();

new lambda.Alias(stack, 'Alias', {
aliasName: 'prod',
version,
});

expect(stack).to(haveResourceLike('AWS::Lambda::Version', {
FunctionName: { Ref: "MyLambdaCCE802FB" },
}));

expect(stack).to(haveResourceLike('AWS::Lambda::Alias', {
FunctionName: { Ref: "MyLambdaCCE802FB" },
Name: "prod"
}));

test.done();
},

'can add additional versions to alias'(test: Test) {
const stack = new Stack();

Expand Down

0 comments on commit 6fc179a

Please sign in to comment.