Skip to content
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): esbuild failed because --external #30080

Open
blanchma opened this issue May 6, 2024 · 6 comments
Open

(aws_lambda_nodejs): esbuild failed because --external #30080

blanchma opened this issue May 6, 2024 · 6 comments
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@blanchma
Copy link

blanchma commented May 6, 2024

Describe the bug

Running the following CDK construct:

new aws_lambda_nodejs.NodejsFunction(this, 'RollbarNotifierLambda', { projectRoot: path.join(__dirname, '../assets/RollbarNotifier/'), entry: path.join(__dirname, '../assets/RollbarNotifier/index.js'), functionName: ${envName}-rollbar-notifier, handler: 'index.handler', memorySize: 1024, runtime: lambda.Runtime.NODEJS_20_X, timeout: Duration.seconds(300), depsLockFilePath: path.join(__dirname, '../assets/RollbarNotifier/yarn.lock') });

I got:

Error: Failed to bundle asset SodEtlStack/RollbarNotifierLambda/Code/Stage, bundle output is located at /Users/blanchma/stackwell/sod-ingestion-pipeline/sod-etl/cdk.out/bundling-temp-76055022e26f668566fb894525a4479d0199145c9f6941468063ea115c5cfab1-error: Error: bash -c yarn run esbuild --bundle "/Users/blanchma/stackwell/sod-ingestion-pipeline/sod-etl/assets/RollbarNotifier/index.js" --target=node20 --platform=node --outfile="/Users/blanchma/stackwell/sod-ingestion-pipeline/sod-etl/cdk.out/bundling-temp-76055022e26f668566fb894525a4479d0199145c9f6941468063ea115c5cfab1/index.js" --external:@aws-sdk/* run in directory /Users/blanchma/stackwell/sod-ingestion-pipeline/sod-etl/assets/RollbarNotifier/ exited with status 1

esbuild --version 0.20.2

If I run the command as shown in the error without --external:@aws-sdk/* it works fine.

Expected Behavior

CDK bundle and deploy the function.

Current Behavior

The esbuild command line seems to be built incorrectly.

Reproduction Steps

Build a function using NodeJsFunction.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.139.0

Framework Version

No response

Node.js Version

18.19.1

OS

macos 12.7

Language

TypeScript

Language Version

No response

Other information

No response

@blanchma blanchma added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 6, 2024
@scanlonp
Copy link
Contributor

scanlonp commented May 6, 2024

I will look into this.

To work around this, you can set external modules to [] or set bundleAwsSDK to true. See the readme here https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk-lib/aws-lambda-nodejs#externals.

@blanchma
Copy link
Author

blanchma commented May 6, 2024

It seems the issue is related to esbuild not found when CDK runs it

@ashishdhingra
Copy link
Contributor

ashishdhingra commented May 7, 2024

@blanchma Good morning. Please advise if you were able to resolve the issue. With NODEJS_20_X Lambda runtime, AWS JavaScript SDK is not included in the runtime and needs to be be bundled during deployment.

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels May 7, 2024
@scanlonp
Copy link
Contributor

scanlonp commented May 7, 2024

@ashishdhingra, why do you say that the SDK is not included in the NODEJS_20_X Lambda runtime? This blog post says it is.

@ashishdhingra
Copy link
Contributor

@ashishdhingra, why do you say that the SDK is not included in the NODEJS_20_X Lambda runtime? This blog post says it is.

@scanlonp My bad, I meant AWS SDK for JavaScript v2. Apologies for the confusion.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 7, 2024
@khushail khushail added p2 effort/medium Medium work item – several days of effort labels Jul 31, 2024
@HknLof
Copy link

HknLof commented Aug 16, 2024

@ashishdhingra If it helps. It seems to be an error when escaping the --external:@aws-sdk/* flag.

This gets printed by cdk synth:

bash -c yarn run esbuild --bundle "/Users/hakanlofcali/Code/TTS/cai/src/ingestion/lambda/enrichment/geoIPMapping/index.ts" --target=node20 --platform=node --outfile="/Users/hakanlofcali/Code/TTS/cai/cdk.out/bundling-temp-81a140cc6448b9c92ca0c55ebad23426471e0b80b5b4ee0bd21e178a8d94ffbb/index.js" --external:@aws-sdk/*

What works is:

bash -c yarn run esbuild --bundle "/Users/hakanlofcali/Code/TTS/cai/src/ingestion/lambda/enrichment/geoIPMapping/index.ts" --target=node20 --platform=node --outfile="/Users/hakanlofcali/Code/TTS/cai/cdk.out/bundling-temp-81a140cc6448b9c92ca0c55ebad23426471e0b80b5b4ee0bd21e178a8d94ffbb/index.js" --external:"@aws-sdk/*"

Preserving literal string by double quoting seems to help.
--external:@aws-sdk/* -> --external:"@aws-sdk/*".

Note

Below seems to be the problem. Not sure, I do not know the code base. Trying to help :)

const versionedExternals = isV2Runtime ? ['aws-sdk'] : ['@aws-sdk/*'];

    const versionedExternals = isV2Runtime ? ['aws-sdk'] : ['@aws-sdk/*'];

to

   const versionedExternals = isV2Runtime ? ['aws-sdk'] : ['"@aws-sdk/*"'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

5 participants