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

fix(lambda): cannot use latest version in multiple cloudfront distributions #9966

Merged
merged 4 commits into from
Aug 26, 2020
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
8 changes: 6 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ export abstract class FunctionBase extends Resource implements IFunction {
*/
protected _connections?: ec2.Connections;

private _latestVersion?: LatestVersion;

/**
* Adds a permission to the Lambda resource policy.
* @param id The id ƒor the permission construct
Expand Down Expand Up @@ -244,8 +246,10 @@ export abstract class FunctionBase extends Resource implements IFunction {
}

public get latestVersion(): IVersion {
// Dynamic to avoid infinite recursion when creating the LatestVersion instance...
return new LatestVersion(this);
if (!this._latestVersion) {
this._latestVersion = new LatestVersion(this);
}
return this._latestVersion;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,31 @@ export = testCase({
test.done();
},

'multiple calls to latestVersion returns the same version'(test: Test) {
const stack = new cdk.Stack();

const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NODEJS_10_X,
});

const version1 = fn.latestVersion;
const version2 = fn.latestVersion;

const expectedArn = {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['MyLambdaCCE802FB', 'Arn'] },
':$LATEST',
]],
};
test.equal(version1, version2);
test.deepEqual(stack.resolve(version1.functionArn), expectedArn);
test.deepEqual(stack.resolve(version2.functionArn), expectedArn);

test.done();
},

'currentVersion': {
// see test.function-hash.ts for more coverage for this
'logical id of version is based on the function hash'(test: Test) {
Expand Down