-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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/ |
Hey @WtfJoke, I am glad there is a workaround for this issue, but I am hesitant to make it the default behavior. Setting this default value for I am most comfortable with a user setting I am concerned that this issue comes from such a basic configuration: do not bundle the SDK and use ESM. If necessary, we could narrow a fix in the CDK to only this configuration. Say checking if external modules is empty and the output format is ESM, then setting That would add logic here:
and this line becomes:
I will check with the team, but I would prefer the workaround unless this issue is run into by many users. In any case, I will keep this open so that people can find the workaround more easily. |
try using https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.592.0 or later of the AWS SDK. We (AWS SDK) recently added conditional exports (package.json exports) in two of the SDK's core dependencies, and it needed a patch for bundlers in Node.js specifically, to add the non-spec "module" field recognizable by esbuild. |
To verify if aws/aws-cdk#29310 (comment) fixes the issue. Sadly it does not 🥲
@kuhe I've update to the latest available version 3.600.0 (see reproducer), but sadly this doesnt fix the issue @scanlonp First of thanks for your detailed answer/analysis.
Looking forward to have it fixed there.
Just a comment to that: I would also be interested on how people use this
Appreciate your openness about a solution 😄 However I'm hesitant to say that we should follow this route. |
## Proposed changes [OLH-1954] Upgrade esbuild to v0.23 ### What changed Added more flags to the Metadata sections for each lambda based on AWS recommendations I have also added an additional Banner options to address the following error: ``` "Error: Dynamic require of \"os\" is not supported", " at file:///var/task/write-activity-log.mjs:1:479", " at exports (/tmp/tmp2g39fyph/node_modules/@smithy/shared-ini-file-loader/dist-cjs/getHomeDir.js:4:14)", " at file:///var/task/write-activity-log.mjs:1:590", " at <anonymous> (/tmp/tmp2g39fyph/node_modules/@smithy/shared-ini-file-loader/dist-cjs/index.js:33:25)", " at file:///var/task/write-activity-log.mjs:1:590", " at <anonymous> (/tmp/tmp2g39fyph/node_modules/@smithy/node-config-provider/dist-cjs/index.js:65:37)", " at file:///var/task/write-activity-log.mjs:1:590", " at exports (/tmp/tmp2g39fyph/node_modules/@smithy/middleware-endpoint/dist-cjs/adaptors/getEndpointFromConfig.js:4:32)", " at file:///var/task/write-activity-log.mjs:1:590", " at <anonymous> (/tmp/tmp2g39fyph/node_modules/@smithy/middleware-endpoint/dist-cjs/index.js:101:36)" ``` ### Why did it change To address https://govukverify.atlassian.net/browse/INCIDEN-839 ### Related links https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html Known AWS CDK issue aws/aws-cdk#29310 ## Checklists <!-- Merging this PR deploys to production. Please answer accurately. --> ### Environment variables or secrets - [ ] No environment variables or secrets were added or changed <!-- Delete if changes DO NOT include new environment variables or secrets --> - [ ] Added parameters to Cloudformation template ### Permissions - [ ] This PR adds or changes permissions ## Testing - Refactored `write-activity-log` handler to log any errors, for testing purposes only - AWS_LAMBDA_EXEC_WRAPPER was removed from the template to avoid output /opt/dynatrace not found errors - Deployed to dev and called `write-activity-log` lambda - Checked for any errors and 200 status <img width="1621" alt="image" src="https://github.com/govuk-one-login/di-account-management-backend/assets/3810819/42f49abf-54c8-4e85-b264-dceeb3c50e67"> ## How to review Deploy this branch to dev and manually test the lambda with this fixture https://github.com/govuk-one-login/di-account-management-backend/blob/main/post-deploy-tests/fixtures/write-activity-log.json [OLH-1954]: https://govukverify.atlassian.net/browse/OLH-1954?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
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: