AWS Compute Blog
Optimizing Node.js dependencies in AWS Lambda
This post is written by Richard Davison, Senior Partner Solutions Architect.
AWS Lambda offers support for Node.js versions 12, 14 and recently announced version 16. Since Node.js parses, optimizes and runs JavaScript on-the-fly, it can provide fast startup and low overhead in a serverless environment.
Node.js reads and parses all dependencies and sources that are required or imported from the entry point. Consequently, it’s important to keep the dependencies to a minimum and optimize the ones in use.
This post shows how to bundle and minify Lambda function code to optimize performance and stay up to date with the latest version of your dependencies.
Understanding Node.js module resolution
When you require or import a resource in your code, Node.js tries to resolve that resource by either the file- or directory name, or in the node_modules directory. Once it finds the resource, it is loaded from disk, parsed and run.
If that file or dependency in turn contains other imports or require statements, the process repeats, which causes disk reads. The more dependencies and files that are imported in a function, the longer it takes to initialize.
This only impacts imported and used code. Including files in a project that are not imported or used has minimal effect on startup performance.
You should also evaluate what’s being imported. Even though modern JavaScript bundlers such as esbuild, Rollup, or WebPack uses tree shaking and dead code elimination, importing dependencies via wildcard, global-, or top-level imports can result in larger bundles.
Use path imports if your library supports it:
//es6
import DynamoDB from "aws-sdk/clients/dynamodb"
//es5
const DynamoDB = require("aws-sdk/clients/dynamodb")
Avoid wildcard imports:
//es6
import {* as AWS} from "aws-sdk"
//es5
const AWS = require("aws-sdk")
Avoid top-level imports:
//es6
import AWS from "aws-sdk"
//es5
const AWS = require("aws-sdk")
AWS SDK for JavaScript v3
The documentation shows that all Node.js runtimes share the same AWS SDK for JavaScript version. To control the version of the SDK that you depend on, you must provide it yourself. Consider using AWS SDK V3, which uses a modular architecture with a separate package for each service.
This has many benefits, including faster installations and smaller deployment sizes. It also includes many frequently requested features, such as a first-class TypeScript support and a new middleware stack. Since there is a separate package for each service, top-level import is not possible, which further increases startup performance.
By providing your own AWS SDK, it can also be bundled and minified during the build process, which can result in cold start reduction.
Bundle and minify Node.js Lambda functions
You can bundle and minify Lambda functions by using esbuild. This is one of the fastest JavaScript bundlers available, often 10-100x faster than alternatives like WebPack or Parcel.
To use esbuild:
1. Add esbuild to your dev dependencies using npm or yarn:
- npm:
npm i esbuild --save-dev
- yarn:
yarn add esbuild --dev
2. Create a “build” script in the script section of the package.json file:
"scripts": {
"build": "rm -rf dist && esbuild ./src/* --entry-names=[dir]/[name]/index --bundle --minify --sourcemap --platform=node --target=node16.14 --outdir=dist",
}
This script first removes the dist directory and then runs esbuild with the following command-line arguments:
./src/*
First, specify the entry points of the application. esbuild creates one bundle (when the bundle option is enabled) for each entry point provided, containing only the dependencies it uses.--entry-names=[dir]/[name]/index
specifies that esbuild should create bundles in the same directory as its entry point and in a directory with the same name as the entry point. The bundle is then namedindex.js
.--bundle
indicates that you want to bundle all dependencies and source code in a single file.--minify
is used to minify the code.--sourcemap
is used to create a source map file, which is essential for debugging minified code. Since the minified code is different from your source code, a source map enables a JavaScript debugger to map the minified code to the original source code. Generating source maps helps debugging but increases the size. Note that source maps must be registered to be applied. To register source maps in a Lambda function, use theNODE_OPTIONS
environment variable with the following value:--enable-source-maps
--platform=node
and--target=node16.14
are used to indicate the ECMAScript version to target. By using a bundler, you can often compile newer JavaScript features and syntaxes to earlier standards. Since Lambda now supports Node.js 16, set the target tonode16.14
. For reference, use https://node.green/ to compare Node.js versions with ECMAScript features.--outdir=dist
indicates that all files should be placed in thedist
directory.
Build
Run the build script by running yarn build
or npm run build
.
Package and deploy
To package your Lambda functions, navigate to the dist directory and zip the contents of each respective directory. Note that one zip file per function should be created, only containing index.js and index.js.map. You may also clone the sample project.
If you are already using the AWS CDK, consider using the NodejsFunction construct. This construct abstracts away the bundle procedure and internally uses esbuild to bundle the code:
const nodeJsFunction = new lambdaNodejs.NodejsFunction(
this,
"NodeJsFunction",
{
runtime: lambda.Runtime.NODEJS_16_X,
handler: "main",
entry: "../path/to/your/entry.js_or_ts",
}
);
Build and deploy sample project
Once all the sources have been bundled you may have noticed that they have small file sizes compared to zipping node_modules and the source files. Your package may be more than 100x smaller. They will also initialize faster.
- Clone the sample project and, install the dependencies, build the project and package the application by running the following commands:
npm install npm run build npm run package npm run package:unbundled
This produces zip artifacts in the dist directory as well as in the project root. Comparing the size difference between dist/ddbHandler.zip and unoptimized.zip, the unbundled artifact is more than ten times larger. When unpacked, the code size with dependencies is more than 19 Mb compared to 2.1 Mb for the bundled and minified example.
This is significant in the ddbHandler example because of the AWS SDK DynamoDB dependencies, which contains multiple files and resources.
- To deploy the application, run:
npm run deploy
Comparing and measuring the results
After deployment, you can also see a significant cold start performance improvement. You can load test the Lambda functions using Artillery. Replace the url from the deployment output:
Load test unbundled
artillery run -t "https://{YOUR_ID_HERE}.execute-api.eu-west-1.amazonaws.com" -v '{ "url": "/x86/v2-top-level-unbundled" }' loadtest.yml
Load test bundled
artillery run -t "https://{YOUR_ID_HERE}.execute-api.eu-west-1.amazonaws.com" -v '{ "url": "/x86/v3" }' loadtest.yml
View results in CloudWatch Insights by selecting the two functions’ log groups and running the following query:
filter @type = "REPORT"
| parse @log /\d+:\/aws\/lambda\/[\w\d]+-(?<function>[\w\d]+)-[\w\d]+/
| stats
count(*) as invocations,
pct(@duration+greatest(@initDuration,0), 0) as p0,
pct(@duration+greatest(@initDuration,0), 25) as p25,
pct(@duration+greatest(@initDuration,0), 50) as p50,
pct(@duration+greatest(@initDuration,0), 75) as p75,
pct(@duration+greatest(@initDuration,0), 90) as p90,
pct(@duration+greatest(@initDuration,0), 95) as p95,
pct(@duration+greatest(@initDuration,0), 99) as p99,
pct(@duration+greatest(@initDuration,0), 100) as p100
group by function, ispresent(@initDuration) as coldstart
| sort by function, coldstart
The cold start invocations for DdbV3X86 run in 551 ms versus DdbVZTopLevelX86Unbundled, which run in 945 ms (p90). The minified and bundled v3 version has about 1.7x faster cold starts, while also providing faster performance during warm invocations.
Conclusion
In this post, you learn how to improve Node.js cold start performance by up to 70% by bundling and minifying your code. You also learned how to provide a different version of AWS SDK for JavaScript and that dependencies and how they are imported affects the performance of Node.js Lambda functions. To achieve the best performance, use AWS SDK V3, bundle and minify your code, and avoid top-level imports.
For more serverless learning resources, visit Serverless Land.