Skip to content

(core): CustomResourceProvider entrypoint does not pass Lambda context object #18055

@JeffSidedDie

Description

@JeffSidedDie

What is the problem?

As of CDKv2, the CustomResourceProvider class significantly changed its API. In v1, you separately instantiated a Lambda function, then created a CustomResourceProvider via CustomResourceProvider.fromLambda(). In v2, CustomResourceProvider now wraps the Lambda on your behalf, and you specify a code directory to bundle. This is done because an __entrypoint__.js is injected into that bundle, that wraps your custom Lambda code. __entrypoint__.js is generated from packages/@aws-cdk/core/lib/custom-resource-provider/nodejs-entrypoint.ts. Line 41 requires your Lambda handler and line 42 calls it. Currently however the only argument being passed is the event.

// invoke the user handler. this is intentionally inside the try-catch to
// ensure that if there is an error it's reported as a failure to
// cloudformation (otherwise cfn waits).
// eslint-disable-next-line @typescript-eslint/no-require-imports
const userHandler: Handler = require(external.userHandlerIndex).handler;
const result = await userHandler(event);

This breaks any CustomResourceProvider handlers that rely on properties or methods from the context object that is normally an argument for Lambda handlers.

Reproduction Steps

  1. Write a CustomResourceProvider with Lambda code referencing context (e.g. context.getRemainingTimeInMillis()).
  2. Write a resource using that CustomResourceProvider.
  3. Deploy your CDK stack.
  4. The Lambda will error with a TypeError: Cannot read property 'getRemainingTimeInMillis' of undefined and the stack will rollback.

What did you expect to happen?

I expected my stack to deploy successfully as it did with CDKv1.

What actually happened?

Custom resource Lambda threw TypeError: Cannot read property 'getRemainingTimeInMillis' of undefined and stack rollback or fails to deploy.

CDK CLI Version

2.1.0

Framework Version

No response

Node.js Version

16.13.1

OS

Windows 10

Language

Typescript

Language Version

4.5.4

Other information

This restricts other packages from migrating to CDKv2, such as https://github.com/udondan/aws-cloudformation-custom-resource.

Metadata

Metadata

Assignees

Labels

@aws-cdk/coreRelated to core CDK functionalitybugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.p1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions