Skip to content

Commit 8198884

Browse files
authored
fix(lambda): version.fromVersionArn creates invalid Version object (#29820)
### Issue # (if applicable) Closes #29813 ### Reason for this change improve the fromFunctionArn() to better handle the input ARN ### Description of changes fromFunctionArn() does not handle the ARN correctly if the input ARN has trailing version or alias. ### Description of how you validated changes See unit tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f14b60f commit 8198884

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

packages/aws-cdk-lib/aws-lambda/lib/function.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,18 @@ export class Function extends FunctionBase {
671671
* in the same account and region as the stack you are importing it into.
672672
*/
673673
public static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction {
674-
return Function.fromFunctionAttributes(scope, id, { functionArn });
674+
/**
675+
* If the functionArn has a trailing version or alias (more than 7 parts when split by ":",
676+
* we trim off the trailing version/alias to retrieve the real functionArn.
677+
* See lambda resource ARN format here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html
678+
*/
679+
const parts = functionArn.split(':');
680+
if (parts.length > 7) {
681+
const _functionArn = parts.slice(0, 7).join(':');
682+
return Function.fromFunctionAttributes(scope, id, { functionArn: _functionArn });
683+
} else {
684+
return Function.fromFunctionAttributes(scope, id, { functionArn });
685+
}
675686
}
676687

677688
/**

packages/aws-cdk-lib/aws-lambda/test/function.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,30 @@ describe('function', () => {
397397
expect(imported.functionName).toEqual('ProcessKinesisRecords');
398398
});
399399

400+
test('fromFunctionArn with verionArn as the input', () => {
401+
// GIVEN
402+
const stack2 = new cdk.Stack();
403+
404+
// WHEN
405+
const imported = lambda.Function.fromFunctionArn(stack2, 'Imported', 'arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords:1');
406+
407+
// THEN
408+
expect(imported.functionArn).toEqual('arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords');
409+
expect(imported.functionName).toEqual('ProcessKinesisRecords');
410+
});
411+
412+
test('fromFunctionArn with trailing alias as the input', () => {
413+
// GIVEN
414+
const stack2 = new cdk.Stack();
415+
416+
// WHEN
417+
const imported = lambda.Function.fromFunctionArn(stack2, 'Imported', 'arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords:TEST');
418+
419+
// THEN
420+
expect(imported.functionArn).toEqual('arn:aws:lambda:us-east-1:123456789012:function:ProcessKinesisRecords');
421+
expect(imported.functionName).toEqual('ProcessKinesisRecords');
422+
});
423+
400424
test('Function.fromFunctionName', () => {
401425
// GIVEN
402426
const stack = new cdk.Stack();

packages/aws-cdk-lib/aws-lambda/test/lambda-version.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ describe('lambda version', () => {
1515
// WHEN
1616
const version = lambda.Version.fromVersionArn(stack, 'Version', 'arn:aws:lambda:region:account-id:function:function-name:version');
1717

18+
expect(version.version).toStrictEqual('version');
19+
expect(version.lambda.functionArn).toStrictEqual('arn:aws:lambda:region:account-id:function:function-name');
20+
1821
new cdk.CfnOutput(stack, 'ARN', { value: version.functionArn });
1922
new cdk.CfnOutput(stack, 'Name', { value: version.functionName });
2023

0 commit comments

Comments
 (0)