jeremydaly / lambda-api

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

[Feature Request] Add support for aws-sdk-js-v3 (modularization!) #192

Closed adworacz closed 1 year ago

adworacz commented 2 years ago

Just wanted to open this up, since lambda-api prides itself on being "zero dependencies".

When starting out on a new project with lambda-api, I realized that my build started failing because there is a dependency, on the aws-sdk. Unfortunately, webpack can only do so much with the older v2 version of the aws-sdk, and my Lambda size has ballooned to 6.5MB with basically no code.

The v3 version of the AWS SDK should drastically improve this, as they broke up the code into multiple modules. This allows us to import only the S3 client whenever we just want to use S3.

Considering that S3 seems to be the only service lambda-api depends on, this should drastically shrink the Lambda size.

I suppose the other option is to remove the S3 integration entirely. If S3 integration is kept, then we should likely remove this line, as lambda-api does in fact have dependencies.

Thanks for the excellent work on this project! Look forward to hearing from you.

warapitiya commented 2 years ago

You don't need to bundle aws-sdk into the lambda. AWS lambda node runtime comes with aws-sdk module pre-installed.

adworacz commented 2 years ago

@warapitiya Got it. I just tested this out using Webpack Externals.

Made the following change in my webpack.config.js file:

module.exports = (env) => ({
  ...
  externals: {
    'aws-sdk': 'aws-sdk',
  },
})

Dropped my Lambda size from 6.5MB down to 33K. Massive improvement!

I think this issue can be close, although it might be a good idea to update the documentation to either reference this issue or directly mention how to use "Externals".

I'm happy to provide a pull request to do so, if desired.

cshenrik commented 1 year ago

As Yan Cui describes here, you should probably bundle the aws sdk instead of relying on the one provided by the Lambda runtime.

pgelinas commented 1 year ago

Given that the S3 dependency is only for some convenience methods (see 1 and 2), it seems bad to have to depend on the whole old SDK for a feature you might not even need.

I personally ended up putting that as a "dev" dependency in our project and relying on the bundled SDK in the docker image so the code doesn't explode in production on a missing dependency I don't even need.

If I could suggest to:

  1. Update the dependency to @aws-sdk/s3, that's the 3.x module
  2. Only do the const S3 = require('./s3-service'); in the 2 code path the deal with S3, so it's now an "optional" dependency
  3. Update your documentation mentioning this optional dependency, so for folks that bundle their own SDK (like recommended by AWS) it is not a surprise.
pgelinas commented 1 year ago

Great to see https://github.com/jeremydaly/lambda-api/pull/216 was merged! I think this issue can be closed, I'm upgrading to 1.0.1 :slightly_smiling_face:

Thanks!