From 898f4cd0321b4eaed27f1868d35e90bb65345d01 Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Tue, 25 Aug 2020 16:16:12 +0100 Subject: [PATCH 1/2] fix(lambda): cannot use latest version in multiple CloudFront distributions The error produced is around construct collision, i.e., "There is already a Construct with name '$LATEST' in Function". The fix is to cache the latest version on the instance. fixes #4459 --- .../@aws-cdk/aws-lambda/lib/function-base.ts | 7 +++++- .../@aws-cdk/aws-lambda/test/test.function.ts | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/function-base.ts b/packages/@aws-cdk/aws-lambda/lib/function-base.ts index 02b0e5d3285b1..d54065343ec44 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function-base.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function-base.ts @@ -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 @@ -245,7 +247,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; } /** diff --git a/packages/@aws-cdk/aws-lambda/test/test.function.ts b/packages/@aws-cdk/aws-lambda/test/test.function.ts index 352d438e9c547..94c878161221b 100644 --- a/packages/@aws-cdk/aws-lambda/test/test.function.ts +++ b/packages/@aws-cdk/aws-lambda/test/test.function.ts @@ -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) { From ecbc935aae042bd56ac9aa10e207ac6a42d8d2e2 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Wed, 26 Aug 2020 23:00:23 +0300 Subject: [PATCH 2/2] Update function-base.ts --- packages/@aws-cdk/aws-lambda/lib/function-base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/@aws-cdk/aws-lambda/lib/function-base.ts b/packages/@aws-cdk/aws-lambda/lib/function-base.ts index d54065343ec44..8cbc0f241863a 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function-base.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function-base.ts @@ -246,7 +246,6 @@ export abstract class FunctionBase extends Resource implements IFunction { } public get latestVersion(): IVersion { - // Dynamic to avoid infinite recursion when creating the LatestVersion instance... if (!this._latestVersion) { this._latestVersion = new LatestVersion(this); }