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): Wrong @aws-sdk bundling when using format: OutputFormat.ESM and externalModules: [] #29310
Comments
This would need further investigation. I'm making it a p1 but we welcome any pull requests from the community. |
Following settings works for me: const banner =
"const require = (await import('node:module')).createRequire(import.meta.url);const __filename = (await import('node:url')).fileURLToPath(import.meta.url);const __dirname = (await import('node:path')).dirname(__filename);";
const externalModules = [
'@aws-sdk/credential-provider-cognito-identity',
'@aws-sdk/credential-provider-http',
'@aws-sdk/credential-provider-ini',
'@aws-sdk/credential-provider-process',
'@aws-sdk/credential-provider-sso',
'@aws-sdk/credential-provider-web-identity',
'@smithy/credential-provider-imds',
];
new nodejs.NodejsFunction(this, 'Function', {
entry: 'function.ts',
runtime: lambda.Runtime.NODEJS_20_X,
bundling: {
banner,
externalModules,
format: nodejs.OutputFormat.ESM,
mainFields: ['module', 'main'],
},
}); The |
Specifying The cdk docs to mainFields outlines that too:
It seems it would make sense to specify It seems also an issue with aws-sdk bundling as well, according to evanw/esbuild#2692 and aws/aws-sdk-js-v3#4217. If I understand the issues correctly, when these would be fixed its not needed to overwrite mainFields. |
Hey @WtfJoke, this is a dependency issue. ESBuild converts There are workarounds for this,
|
Hey @Vandita2020 Yeah I'm currently using the mentioned workarounds ( Have you seen my reproducer here: https://github.com/WtfJoke/cdk-nodejs-esm-bug-reproducer? import { SSM } from "@aws-sdk/client-ssm";
export const handler = async () => {
new SSM().putParameter({
Name: "my-param",
Value: "my-value",
Overwrite: true,
});
}; It only uses Do you think that this issue should be closed in favour of aws/aws-sdk-js-v3#4217 or a new issue in aws-sdk? |
Hey @WtfJoke, |
Hey @Vandita2020 Thanks for taking the time, bundle the code without using CDK and discussing this also with the AWS-SDK team. Appreciate that. Currently I do not understand why your mentioned issue is the cause for that (as far as I see, there is also no response from the esbuild or the aws sdk maintainers there). Is it possible to get a bit more of an explanation here? There exists a previous issue in esbuild for the aws sdk, where the esbuild maintainer responded, concluding that there is an issue in the aws sdk. For me it looks a bit like a deadlock here 🙈 Any ideas how we can progress from here? P.S. For cdk/ |
Describe the bug
We have a project using esModules. We want to utilize esModules also for our Lambdas.
When using
NodeJsFunction
and provide the following custom bundling options:bundling: { externalModules: [], format: OutputFormat.ESM },
the resulting.mjs
file contains wrong imports (dist-cjs instead of dist-es).Here is the section of the bundled
.mjs
fileWhen setting only format option
bundling: { format: OutputFormat.ESM },
, everything works as expected, because the provided aws-sdk is loaded (instead of the bundled one).We set
externalModules: []
because that leads to lower cold start times (reference: #25492), which means the@aws-sdk
is bundled.Expected Behavior
I expect that no CommonJS will be used when setting
OutputFormat.esm
in the bundling options.Current Behavior
The resulting .mjs file contains commonJs references, which leads to the function crashing:
The function crashes with the following exception:
Reproduction Steps
Alternatively:
CdkWorkshopStack-MyLambda
lambda and inspect the errorPossible Solution
No response
Additional Information/Context
No response
CDK CLI Version
10.2.4
Framework Version
No response
Node.js Version
v20.11.0
OS
windows (wsl)
Language
TypeScript
Language Version
5.3.3
Other information
No response
The text was updated successfully, but these errors were encountered: