-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aws-lambda-nodejs: How to setup AWS CDK to use ESM with layers / top level await #23333
Comments
I have a CDK Severless-Stack project template that uses ESM and top-level await here: https://github.com/jetbridge/sst-prisma |
I created a repository here: https://github.com/Nantis-GmbH/cdk-lambda-with-layers to show my approach to lambda and layer bundling. Short summary:
Downsides:
|
Just FYI, our use of the |
Confirmed w/ AWS support
I'll keep requesting escalations w/ AWS Support until I get a solution worth reporting back with. |
@daveharig Did you mean to mention layers somewhere in your list? |
The issue is bigger than Layers. It's any TS CDK project with any Lambda wanting to use ESM at the same time. This Layers case was just the first one to discover it and publicly post about it. This guy sums up the broader issue really well. https://twitter.com/adamdotdev/status/1523282408417677312?s=20 |
Thanks for the link @daveharig. Going by the video, this appears possible already (not the layers stuff maybe). I also agree with the OP that ESM vs CJS is a mess and hard to understand. Would you mind clarifying what your ask is specifically? |
The Twitter workaround is hack-y at best. Depending on the SDK's used in your Lambdas you end up chasing your tail even more. My ask is that the CDK service team provide official documentation on how to support Lambda w/ ESM in a TS CDK project. |
In my case I just changed to |
This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue. |
@pfried can you add aws/aws-lambda-nodejs-runtime-interface-client#93 (comment) to your related issues? |
@ThePlenkov this allowed you to change to |
Of course. Tsx doesn't care |
@ThePlenkov thank you! I'll give it a try. |
could anyone please clarify how to use TSX to get top level await working with Typescript and CDK ? |
This worked for me as well to deploy the cdk project. I haven't tried the top level await yet. I'll report how that goes. |
@domengabrovsek that is fantastic! Would you be willing to share how you have your compiler options set in tsconfig.json? |
I too have been down the ts-node rabbit hole. Apparently using "module": "ESNext" with ts-node requires esm, and the issue here TypeStrong/ts-node#1997 suggests that using esm with ts-node is broken for newer versions of node. When I switched to tsx, things started working. |
swc-node will save your life |
Describe the issue
Lambda is one of the most important services of the AWS portfolio. Significant improvements have been made allowing the NodeJS runtime (14+) to use ESM and top level await:
NODE_PATH
, global fetch, ...)Yet I cannot really use that in my application due to several reasons:
solves this issue since I did not test it ever since. Has anyone?
When having CDK project files with the lambda function handler code side by side in a project you will have issues with typechecking since the
.tsconfig
file shipped withaws-cdk
does not play nice with handler code which needs other settings in.tsconfig
. On the other hand if you change your.tsconfig
file to other target/module/library settings the project will fail (no module errors, if you changepackage.json
to"type"="module" it will not like the
*.ts` extension, etc. etc. its a rabbit hole to which bottom I never got)The state of Lambda Layer bundling is a wild landscape of self-baked solutions (of course I have my own). The issues of this are widespread and maybe better in its own thread)
I would heavily welcome if someone at AWS / AWS-CDK takes a deep dive on how to setup an aws-cdk TypeScript project with ESM / top level await / ESM layers and shows it to the rest of the world. There is too much potential improvement on the line to keep this lingering in its current state (is it only the docs?)
The whole JavaScript ecosystem with CJS / ESM etc. is a mess only few developers will fully understand (excluding me). AWS-CDK is a powerful but complex software (which I love). Implementing the above requires a lot of knowledge (at least when the solution must be compatible with future plans / releases)
Related Issues:
Partial solutions / approaches to some problems and additional content
Links
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs-readme.html
The text was updated successfully, but these errors were encountered: