Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): add a newVersion method #2099

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with the new-lines here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally try to break docs on sentence boundaries, so that future diffs of changes here will look better.

The newlines are not rendered in the docs themselves.

*/
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