Skip to content

(custom-resources): Call out that AwsCustomResource uses SingletonFunction #19309

@ckifer

Description

@ckifer

General Issue

No way to rename log group/lambda for AwsCustomResource

The Question

I'm not sure if this is a feature or bug, sorry if this is in the wrong place.

AwsCustomResource uses a SingletonLambda to make a single SDK call (I didn't, understand this when I started my project). I utilize this a few times in the same stack for a website:

  1. Get my AppMonitor ID for CW RUM (not in CFN spec yet)
  2. Invalidate my CloudFront distribution at the end of deploy
  3. Upload some JSON to my website bucket as config for the site

I created one of these first, say a custom resource of the following form:

  private getAppMonitorId() {
    const awsRUMSDKCall: AwsSdkCall = {
      service: 'RUM',
      action: 'getAppMonitor',
      parameters: { Name: this.rumMonitor.name },
      physicalResourceId: PhysicalResourceId.of('7854f358-6c0a-4ed4-8863-2d5bf35d7062'),
    };

    const customResource = new AwsCustomResource(this, 'Custom::GetAppMonitorId', {
      policy: AwsCustomResourcePolicy.fromSdkCalls({
        resources: [this.rumMonitorArn],
      }),
      installLatestAwsSdk: true,
      onCreate: awsRUMSDKCall,
      onUpdate: awsRUMSDKCall,
      functionName: 'GetAppMonitorCustomResourceHandler',
    });

    customResource.node.addDependency(this.rumMonitor);

    return customResource.getResponseField('AppMonitor.Id');
  }
}

The functionName here names the Singleton Lambda and the log group that gets created /aws/lambda/GetAppMonitorCustomResourceHandler. This is all well and good until another custom resource is added, with a functionName or not. The new custom resource then uses the named lambda and the named log group named specifically for a different custom resource. This creates confusion when trying to debug, trying to look for the logs (especially if you don't know that AwsCustomResource is a singleton lambda), etc.

Out of this, I ask two things:

  1. Is it possible to rename an AwsCustomResource function name?
    • I think the answer to this is no, as I get Modifying service token is not allowed. on any attempts I make.
  2. I would move to suggest to remove/deprecate functionName from AwsCustomResource as the creation of a specifically named lambda that potentially adds unrelated AwsCustomResources to it is confusing. I don't know what implications that removal would have and is probably a breaking change so I'm not too worried about it now that I know. Calling it out at the top of the AwsCustomResource docs page would be nice at least.

CDK CLI Version

2.0.0

Framework Version

1.147.0

Node.js Version

16

OS

Mac OSX Monterey

Language

Typescript

Language Version

4.0.5

Other information

No response

Metadata

Metadata

Labels

documentationThis is a problem with documentation.effort/smallSmall work item – less than a day of effortp2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions