-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
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.
aws-cdk/packages/@aws-cdk/core/lib/custom-resource-provider/nodejs-entrypoint.ts
Lines 37 to 42 in 2b6c2da
| // 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
- Write a
CustomResourceProviderwith Lambda code referencingcontext(e.g.context.getRemainingTimeInMillis()). - Write a resource using that
CustomResourceProvider. - Deploy your CDK stack.
- The Lambda will error with a
TypeError: Cannot read property 'getRemainingTimeInMillis' of undefinedand 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.