Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): add a fromFunctionName() method #19076

Merged
merged 1 commit into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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