Nodejs modularized aws-sdk v3 size getting increased

Viewed 1036

I am trying to reduce size of nodejs lambda bundle which uses aws-sdk. This is the original lambda package.json file:

{
  "name": "lambdanodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.784.0",
    "bluebird": "^3.7.2",
    "ioredis": "^4.19.2",
    "redis": "^3.0.2",
    "redis-clustr": "^1.7.0"
  }
}

Overall size is 57MB, 54 belongs to aws-sdk.

To reduce size I tried using specific client services (v3 sdk). Followed : https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-secrets-manager/package.json

{
  "name": "lambdanodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-dynamodb": "^3.7.0",
    "@aws-sdk/client-secrets-manager": "^3.7.0",
    "bluebird": "^3.7.2",
    "ioredis": "^4.19.2",
    "redis": "^3.0.2",
    "redis-clustr": "^1.7.0"
  }
}

Now npm install results in even larger size around 190+MBs. Also in node_modules I see lot of directories which were not there when using previous package.json install. This v3 aws-sdk is supposed to be lighter. Am I missing something?

Thanks!

1 Answers
  • Don't push aws-sdk to the lambda function. If you want to use it in the local development environment install it globally.
  • Lambda will provide all necessary SDK modules(v2 & v3) during runtime, just import them in the code.
  • Don't bundle the aws-sdk in the zip.
  • If your zip size is getting increased, use layers in lambda.
  • Import the node_moudles to lambda layers.
  • Then upload your code to lambda.
Related