From 0f58e1e9bf2fca6a3c185ea807e8ba406fd3887b Mon Sep 17 00:00:00 2001 From: Pahud Hsieh Date: Fri, 12 Apr 2024 20:19:54 +0000 Subject: [PATCH 1/2] fix fromFunctionArn --- .../aws-cdk-lib/aws-lambda/lib/function.ts | 13 +++++++++- .../aws-lambda/test/function.test.ts | 24 +++++++++++++++++++ .../aws-lambda/test/lambda-version.test.ts | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index dba046f447513..e9e8a1e51cfb7 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -671,7 +671,18 @@ export class Function extends FunctionBase { * in the same account and region as the stack you are importing it into. */ public static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction { - return Function.fromFunctionAttributes(scope, id, { functionArn }); + /** + * First, validate the functionArn string. If it's ARN with trailing version or alias, + * we trim off the trailing to retrieve the real functionArn. + * See lambda resource ARN format here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html + */ + const parts = functionArn.split(':'); + if (parts.length > 7) { + const _functionArn = parts.slice(0, 7).join(':'); + return Function.fromFunctionAttributes(scope, id, { functionArn: _functionArn }); + } else { + return Function.fromFunctionAttributes(scope, id, { functionArn }); + } } /** diff --git a/packages/aws-cdk-lib/aws-lambda/test/function.test.ts b/packages/aws-cdk-lib/aws-lambda/test/function.test.ts index f4c5382707641..7711c3ff0fed3 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/function.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/function.test.ts @@ -397,6 +397,30 @@ describe('function', () => { expect(imported.functionName).toEqual('ProcessKinesisRecords'); }); + test('fromFunctionArn with verionArn as the input', () => { + // GIVEN + const stack2 = new cdk.Stack(); + + // WHEN + const imported = lambda.Function.fromFunctionArn(stack2, 'Imported', 'arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords:1'); + + // THEN + expect(imported.functionArn).toEqual('arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords'); + expect(imported.functionName).toEqual('ProcessKinesisRecords'); + }); + + test('fromFunctionArn with trailing alias as the input', () => { + // GIVEN + const stack2 = new cdk.Stack(); + + // WHEN + const imported = lambda.Function.fromFunctionArn(stack2, 'Imported', 'arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords:TEST'); + + // THEN + expect(imported.functionArn).toEqual('arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords'); + expect(imported.functionName).toEqual('ProcessKinesisRecords'); + }); + test('Function.fromFunctionName', () => { // GIVEN const stack = new cdk.Stack(); diff --git a/packages/aws-cdk-lib/aws-lambda/test/lambda-version.test.ts b/packages/aws-cdk-lib/aws-lambda/test/lambda-version.test.ts index 6236a92df5dae..3356fa2dbf400 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/lambda-version.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/lambda-version.test.ts @@ -15,6 +15,9 @@ describe('lambda version', () => { // WHEN const version = lambda.Version.fromVersionArn(stack, 'Version', 'arn:aws:lambda:region:account-id:function:function-name:version'); + expect(version.version).toStrictEqual('version'); + expect(version.lambda.functionArn).toStrictEqual('arn:aws:lambda:region:account-id:function:function-name'); + new cdk.CfnOutput(stack, 'ARN', { value: version.functionArn }); new cdk.CfnOutput(stack, 'Name', { value: version.functionName }); From 4ff9cf19fdab989f042fce7823c3cc077c4b2741 Mon Sep 17 00:00:00 2001 From: Pahud Hsieh Date: Fri, 12 Apr 2024 17:03:08 -0400 Subject: [PATCH 2/2] Update packages/aws-cdk-lib/aws-lambda/lib/function.ts Co-authored-by: paulhcsun <47882901+paulhcsun@users.noreply.github.com> --- packages/aws-cdk-lib/aws-lambda/lib/function.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index e9e8a1e51cfb7..3f784a5ccc247 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -672,8 +672,8 @@ export class Function extends FunctionBase { */ public static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction { /** - * First, validate the functionArn string. If it's ARN with trailing version or alias, - * we trim off the trailing to retrieve the real functionArn. + * If the functionArn has a trailing version or alias (more than 7 parts when split by ":", + * we trim off the trailing version/alias to retrieve the real functionArn. * See lambda resource ARN format here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html */ const parts = functionArn.split(':');