diff --git a/packages/@aws-cdk/aws-lambda/README.md b/packages/@aws-cdk/aws-lambda/README.md index 44fded6a80609..9c9182b174550 100644 --- a/packages/@aws-cdk/aws-lambda/README.md +++ b/packages/@aws-cdk/aws-lambda/README.md @@ -509,6 +509,14 @@ const fn = lambda.Function.fromFunctionAttributes(this, 'Function', { }); ``` +If `fromFunctionArn()` causes an error related to having to provide an account and/or region in a different construct, +and the lambda is in the same account and region as the stack you're importing it into, +you can use `Function.fromFunctionName()` instead: + +```ts +const fn = lambda.Function.fromFunctionName(this, 'Function', 'MyFn'); +``` + ## Lambda with DLQ A dead-letter queue can be automatically created for a Lambda function by diff --git a/packages/@aws-cdk/aws-lambda/lib/function.ts b/packages/@aws-cdk/aws-lambda/lib/function.ts index 979923c093be0..5bff57e1d43bc 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function.ts @@ -435,6 +435,20 @@ export class Function extends FunctionBase { this._VER_PROPS[propertyName] = locked; } + /** + * Import a lambda function into the CDK using its name + */ + public static fromFunctionName(scope: Construct, id: string, functionName: string): IFunction { + return Function.fromFunctionAttributes(scope, id, { + functionArn: Stack.of(scope).formatArn({ + service: 'lambda', + resource: 'function', + resourceName: functionName, + arnFormat: ArnFormat.COLON_RESOURCE_NAME, + }), + }); + } + /** * Import a lambda function into the CDK using its ARN */ diff --git a/packages/@aws-cdk/aws-lambda/test/function.test.ts b/packages/@aws-cdk/aws-lambda/test/function.test.ts index 9e71b4c0608d0..2ed43bb37d54b 100644 --- a/packages/@aws-cdk/aws-lambda/test/function.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/function.test.ts @@ -298,6 +298,42 @@ describe('function', () => { expect(imported.functionName).toEqual('ProcessKinesisRecords'); }); + test('Function.fromFunctionName', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const imported = lambda.Function.fromFunctionName(stack, 'Imported', 'my-function'); + + // THEN + expect(stack.resolve(imported.functionArn)).toStrictEqual({ + 'Fn::Join': ['', [ + 'arn:', + { Ref: 'AWS::Partition' }, + ':lambda:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':function:my-function', + ]], + }); + expect(stack.resolve(imported.functionName)).toStrictEqual({ + 'Fn::Select': [6, { + 'Fn::Split': [':', { + 'Fn::Join': ['', [ + 'arn:', + { Ref: 'AWS::Partition' }, + ':lambda:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':function:my-function', + ]], + }], + }], + }); + }); + describe('Function.fromFunctionAttributes()', () => { let stack: cdk.Stack; diff --git a/packages/@aws-cdk/core/lib/arn.ts b/packages/@aws-cdk/core/lib/arn.ts index a04f03baf466f..2f6cb68e2dc95 100644 --- a/packages/@aws-cdk/core/lib/arn.ts +++ b/packages/@aws-cdk/core/lib/arn.ts @@ -417,8 +417,6 @@ function parseArnShape(arn: string): 'token' | string[] { // Tokens won't contain ":", so this won't break them. const components = arn.split(':'); - // const [/* arn */, partition, service, /* region */ , /* account */ , resource] = components; - const partition = components.length > 1 ? components[1] : undefined; if (!partition) { throw new Error('The `partition` component (2nd component) of an ARN is required: ' + arn);