2
votes

I'm using webpack on a nodeJs repo with the serverless.com framework for deploying to AWS Lambda functions. Since the Lambda container on AWS already contains aws-sdk, I'm attempting to exclude it from packaging.

The output during the build in fact identifies aws-sdk as excluded:

    [41] ./src/controller/queue.ts 2.57 KiB {3} [built]
[43] ./src/controller/auth.ts 3.76 KiB {1} [built]
    + 30 hidden modules
Serverless: Excluding external modules: aws-sdk@^2.309.0
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: @hapi/joi@^16.1.5, moment-timezone@^0.5.23, axios@^0.19.0, chrome-aws-lambda@^2.0.1, puppeteer-core@^2.0.0, uuid@^2.0.3, memorystream@^0.3.1, lambda-warmer@^1.2.1, jsonwebtoken@^8.5.1, [email protected], js-yaml@^3.12.0
Serverless: Packaging service...

Yet the zip file which is built to be uploaded to Lambda still contains aws-sdk and is making the package almost 60MB. I also watched the folders in .webpack during the build process and noticed that the node_modules folder is in both a dependencies folder and a service folder and aws-sdk is under the service folder. I don't really know what that means though.

Any help is appreciated. It's frustrating that it blatantly says it's excluding it yet it winds up in the zip anyway.

My webpack.config.js is below:

const webpack = require("webpack");
const path = require("path");
const serverlessWebpack = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: serverlessWebpack.lib.entries,
  target: 'node',
  mode: serverlessWebpack.lib.webpack.isLocal ? "development" : "production",
  node: {
    __dirname: true
  },
  devtool: 'source-map',
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: "ts-loader"
          }
        ],
        include: [__dirname],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  output: {
    libraryTarget: "commonjs",
    path: path.join(__dirname, ".webpack"),
    filename: "[name].js"
  },
  plugins: [
    new webpack.EnvironmentPlugin({
      NODE_ENV: "development"
    }),
    new CopyWebpackPlugin([
      { from: 'ormconfig.yml' },
    ])
  ]
};

And in my serverless.yml custom section for the serverless-webpack plugin, my config is:

webpack:
  webpackConfig: webpack.config.js
  includeModules:
    forceExclude:
    - aws-sdk
  packager: 'yarn'

4

4 Answers

4
votes

do you have an indentation issue. see forceExclude and includeModules suppose to be in the same level?

should it be,

webpack:
  webpackConfig: webpack.config.js
  includeModules: true
  forceExclude:
    - aws-sdk
  packager: 'yarn'

Reference:

https://github.com/serverless-heaven/serverless-webpack

1
votes

I have the same problem and I don't understand why aws-sdk is still included in the deployment package, which makes about 50MB! My solution is

webpack:
webpackConfig: webpack.config.js   # Name of webpack configuration file
packager: 'yarn'
includeModules:
  forceExclude:
    - aws-sdk
packagerOptions:
  scripts:
    - rm -rf node_modules/aws-sdk
0
votes

Maybe you can change your webpack.config.js to this form:

const slsw = require('serverless-webpack');
// const nodeExternals = require('webpack-node-externals');

module.exports = {
  target:       'node',
  mode:         slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry:        slsw.lib.entries,
  externals:    { 'aws-sdk': 'commonjs2 aws-sdk' },
  ...
}

This works for me.

0
votes

There's also one more method to reduce the size of your function when using the aws sdk. All you have to do is change how you import stuff.

For example this import:

import AWS from 'aws-sdk';

changed to this:

import CognitoIdentity from '@aws-sdk/client-cognito-identity';

It's what aws calls it "Modular packages".

The example they showcased there reduced their function size from ~817KB to ~23KB!

Check the github repo here