jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.42k stars 126 forks source link

AWS Lambda with Node 18.x and SDK v3 throws errors #213

Closed joelrwilliams12 closed 1 year ago

joelrwilliams12 commented 1 year ago

Currently lambda-api loads 'aws-sdk' in /lib/s3-service.js.

This fails with Node.js 18.x Lambda functions which are pre-loaded with SDK v3 and requires new syntax for loading SDK modules.

This throws the following error:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
        "Require stack:",
        "- /var/task/index.js",
        "- /var/runtime/index.mjs",
        "    at _loadUserApp (file:///var/runtime/index.mjs:1000:17)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)",
        "    at async start (file:///var/runtime/index.mjs:1200:23)",
        "    at async file:///var/runtime/index.mjs:1206:1"
    ]
}

New syntax should be something like:

import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { S3Client, getSignedUrl, GetObjectCommand } from "@aws-sdk/client-s3";

Documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_s3_request_presigner.html

QAnders commented 1 year ago

I hate ESM and that we are forced to upgrade to it, and apparently AWS has taken that path as well with Node18 and SDK V3... :(

Even though CJS modules should be backwards compatible, they seldom are when it comes to "frameworks", e.g. Serverless and all it's plugins. This puts us in a difficult position as we have dependencies that are still CJS only but other forces (e.g. AWS) forces us to ESM.

Sure, ESM will win out essentially but it's kind of like BetaMax won over VHS in the 90'ies... We are being forces to move on...

I'd appreciate if this could be done like other module vendors have, leave the current version (maintained) as CJS, and in case you adopt ESM, create a new major version that will be ESM moving forward...

Thanks for the great package!

AndrewBarba commented 1 year ago

@QAnders This has nothing to do with ESM. The nodejs18.x runtime does not bundle the package aws-sdk, the v2 sdk. Instead it bundles @aws-sdk/**/* which is the v3 version of the sdk. Fixing this issue would simply involve updating lambda-api to require the v3 variants of the sdk instead of the v2 variants. No need to switch to ESM

AndrewBarba commented 1 year ago

PR: https://github.com/jeremydaly/lambda-api/pull/216

slootjes commented 1 year ago

When could this PR be merged?

naorpeled commented 1 year ago

Hey everyone, I'll go over all the open issues and see what can be pushed in the next iteration.

afterwards, I'm planning on upgrading the usage of the AWS SDK to v3 + making the lib fully with TS, instead of separate declaration files.

Will keep you updated 🙏

lucas-subli commented 1 year ago

Just tried to update to Node 18 yesterday 😅

Looking forward for the update.

muralimanohars commented 1 year ago

Hey, When do we expect upgraded version with AWS SDK v3?

naorpeled commented 1 year ago

Hey, When do we expect upgraded version with AWS SDK v3?

Hey, been super super busy couple of weeks at work, going on vacation on Wednesday, will probably start working on it on Thursday/Friday and keep you guys posted.

Sorry that it's taking longer than anticipated 🙏

AndrewBarba commented 1 year ago

@naorpeled any reason you wouldn't merge the PR I put up?

naorpeled commented 1 year ago

@naorpeled any reason you wouldn't merge the PR I put up?

No, will review it asap :)

naorpeled commented 1 year ago

Hey everyone, the move of the S3 service to AWS SDK 3 is now in main, I want to make some small adjustments before releasing v1, will release it asap!

joelrwilliams12 commented 1 year ago

thanks so much this update...just updated the package on one of my Lambda functions and it's running with Node 18.x just fine! ❤️

naorpeled commented 1 year ago

thanks so much this update...just updated the package on one of my Lambda functions and it's running with Node 18.x just fine! ❤️

Awesome!!

slootjes commented 1 year ago

Amazing! I will give it a try next week. Thanks in advance for your hard work!

muralimanohars commented 1 year ago

Thanks for the update. It is working fine with Node 18

lucas-subli commented 1 year ago

Ty for the update. Just update my lambdas, no issues !