Skip to content

Commit

Permalink
feat(lambda): add a fromFunctionName() method (aws#19076)
Browse files Browse the repository at this point in the history
After aws#18255 has been merged, the account and region of the Function's imported ARN is correctly recognized.
Unfortunately, this has surfaced a few cases where that causes problems,
like when using an imported Function as the target of a CodePipeline invoke Action
(the pipeline construct needs to verify that the target Function is in the same account and region as the pipeline).

Add a `Function.fromFunctionName()` method that allows you to import a Function in the same account and region.

Fixes aws#19031

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
skinny85 authored and TikiTDO committed Feb 21, 2022
1 parent 59ec5b8 commit 6d9deac
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda/README.md
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Expand Up @@ -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
*/
Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Expand Up @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/core/lib/arn.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 6d9deac

Please sign in to comment.