Skip to content

Commit

Permalink
feat(custom-resources): fixed Lambda function name (#17670)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
cyuste committed Nov 24, 2021
1 parent 797edbd commit 5710fe5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/custom-resources/README.md
Expand Up @@ -347,6 +347,25 @@ This sample demonstrates the following concepts:
* Non-intrinsic physical IDs
* Implemented in Python


### Customizing Provider Function name

In multi-account environments or when the custom resource may be re-utilized across several
stacks it may be useful to manually set a name for the Provider Function Lambda and therefore
have a predefined service token ARN.

```ts

const myProvider = new cr.Provider(this, 'MyProvider', {
onEventHandler: onEvent,
isCompleteHandler: isComplete,
logRetention: logs.RetentionDays.ONE_DAY,
role: myRole,
providerFunctionName: 'the-lambda-name', // Optional
});

```

## Custom Resources for AWS APIs

Sometimes a single API call can fill the gap in the CloudFormation coverage. In
Expand Down
Expand Up @@ -115,6 +115,15 @@ export interface ProviderProps {
* @default - A default role will be created.
*/
readonly role?: iam.IRole;

/**
* Provider Lambda name.
*
* The provider lambda function name.
*
* @default - CloudFormation default name from unique physical ID
*/
readonly providerFunctionName?: string;
}

/**
Expand Down Expand Up @@ -165,7 +174,7 @@ export class Provider extends CoreConstruct implements ICustomResourceProvider {

this.role = props.role;

const onEventFunction = this.createFunction(consts.FRAMEWORK_ON_EVENT_HANDLER_NAME);
const onEventFunction = this.createFunction(consts.FRAMEWORK_ON_EVENT_HANDLER_NAME, props.providerFunctionName);

if (this.isCompleteHandler) {
const isCompleteFunction = this.createFunction(consts.FRAMEWORK_IS_COMPLETE_HANDLER_NAME);
Expand Down Expand Up @@ -198,7 +207,7 @@ export class Provider extends CoreConstruct implements ICustomResourceProvider {
};
}

private createFunction(entrypoint: string) {
private createFunction(entrypoint: string, name?: string) {
const fn = new lambda.Function(this, `framework-${entrypoint}`, {
code: lambda.Code.fromAsset(RUNTIME_HANDLER_PATH),
description: `AWS CDK resource provider framework - ${entrypoint} (${this.node.path})`.slice(0, 256),
Expand All @@ -210,6 +219,7 @@ export class Provider extends CoreConstruct implements ICustomResourceProvider {
vpcSubnets: this.vpcSubnets,
securityGroups: this.securityGroups,
role: this.role,
functionName: name,
});

fn.addEnvironment(consts.USER_ON_EVENT_FUNCTION_ARN_ENV, this.onEventHandler.functionArn);
Expand Down
Expand Up @@ -397,3 +397,27 @@ describe('role', () => {
});
});
});

describe('name', () => {
it('uses custom name when present', () => {
// GIVEN
const stack = new Stack();
const providerFunctionName = 'test-name';

// WHEN
new cr.Provider(stack, 'MyProvider', {
onEventHandler: new lambda.Function(stack, 'MyHandler', {
code: lambda.Code.fromAsset(path.join(__dirname, './integration-test-fixtures/s3-file-handler')),
handler: 'index.onEvent',
runtime: lambda.Runtime.NODEJS_10_X,
}),
providerFunctionName,
});

// THEN
expect(stack).toHaveResourceLike('AWS::Lambda::Function', {
FunctionName: providerFunctionName,
});
});
});

0 comments on commit 5710fe5

Please sign in to comment.