Skip to content

Commit 5710fe5

Browse files
authoredNov 24, 2021
feat(custom-resources): fixed Lambda function name (#17670)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 797edbd commit 5710fe5

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed
 

‎packages/@aws-cdk/custom-resources/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,25 @@ This sample demonstrates the following concepts:
347347
* Non-intrinsic physical IDs
348348
* Implemented in Python
349349

350+
351+
### Customizing Provider Function name
352+
353+
In multi-account environments or when the custom resource may be re-utilized across several
354+
stacks it may be useful to manually set a name for the Provider Function Lambda and therefore
355+
have a predefined service token ARN.
356+
357+
```ts
358+
359+
const myProvider = new cr.Provider(this, 'MyProvider', {
360+
onEventHandler: onEvent,
361+
isCompleteHandler: isComplete,
362+
logRetention: logs.RetentionDays.ONE_DAY,
363+
role: myRole,
364+
providerFunctionName: 'the-lambda-name', // Optional
365+
});
366+
367+
```
368+
350369
## Custom Resources for AWS APIs
351370

352371
Sometimes a single API call can fill the gap in the CloudFormation coverage. In

‎packages/@aws-cdk/custom-resources/lib/provider-framework/provider.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ export interface ProviderProps {
115115
* @default - A default role will be created.
116116
*/
117117
readonly role?: iam.IRole;
118+
119+
/**
120+
* Provider Lambda name.
121+
*
122+
* The provider lambda function name.
123+
*
124+
* @default - CloudFormation default name from unique physical ID
125+
*/
126+
readonly providerFunctionName?: string;
118127
}
119128

120129
/**
@@ -165,7 +174,7 @@ export class Provider extends CoreConstruct implements ICustomResourceProvider {
165174

166175
this.role = props.role;
167176

168-
const onEventFunction = this.createFunction(consts.FRAMEWORK_ON_EVENT_HANDLER_NAME);
177+
const onEventFunction = this.createFunction(consts.FRAMEWORK_ON_EVENT_HANDLER_NAME, props.providerFunctionName);
169178

170179
if (this.isCompleteHandler) {
171180
const isCompleteFunction = this.createFunction(consts.FRAMEWORK_IS_COMPLETE_HANDLER_NAME);
@@ -198,7 +207,7 @@ export class Provider extends CoreConstruct implements ICustomResourceProvider {
198207
};
199208
}
200209

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

215225
fn.addEnvironment(consts.USER_ON_EVENT_FUNCTION_ARN_ENV, this.onEventHandler.functionArn);

‎packages/@aws-cdk/custom-resources/test/provider-framework/provider.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,27 @@ describe('role', () => {
397397
});
398398
});
399399
});
400+
401+
describe('name', () => {
402+
it('uses custom name when present', () => {
403+
// GIVEN
404+
const stack = new Stack();
405+
const providerFunctionName = 'test-name';
406+
407+
// WHEN
408+
new cr.Provider(stack, 'MyProvider', {
409+
onEventHandler: new lambda.Function(stack, 'MyHandler', {
410+
code: lambda.Code.fromAsset(path.join(__dirname, './integration-test-fixtures/s3-file-handler')),
411+
handler: 'index.onEvent',
412+
runtime: lambda.Runtime.NODEJS_10_X,
413+
}),
414+
providerFunctionName,
415+
});
416+
417+
// THEN
418+
expect(stack).toHaveResourceLike('AWS::Lambda::Function', {
419+
FunctionName: providerFunctionName,
420+
});
421+
});
422+
});
423+

0 commit comments

Comments
 (0)
Please sign in to comment.