What /u/Alfred17686 said regarding the use of a bundler is indeed the best approach.
The simple approach using --production installation
Many years ago, I started off doing something similar to what you had. I would have my typescript code compile output to a separate build folder and then I’d cp the package.json into there and run npm install --prefix ./build --production.
In my npm scripts, that looked something like:
“prebuild”: “rm -rf build && npm run test”,
“build”: “mkdir -p build && cp package*.json build/ && tsc --pretty”,
“postbuild”: “npm install --prefix ./build --production”,
Then I’d just have my SAM template point to the build folders and run my deploy script which looked something like:
#!/bin/bash
set -e
set -x
# Build the lambda function
cd ./myLambda
npm run build
cd -
# Check to make sure stack definition is valid
sam validate
# Package template and upload code to S3
sam package --template-file template.yaml --s3-bucket $YOUR_SAM_BUCKET_NAME --output-template-file packaged-template.yml
# Trigger CloudFormation change set deployment
sam deploy --template-file packaged-template.yml --stack-name $YOUR_STACK_NAME --capabilities CAPABILITY_IAM
SAM took care of the zipping and uploading of your code artifact. One important thing to remember with this approach is to make aws-sdk a devDependency if you’re looking to use the 2.x variant. This is because the Node 14 and below runtimes (which are all that exist today) include it on disk already.
The better approach: Bundling
We can do better than the approach above using code bundling. It has many advantages:
- The cold start can load a single file and skip a lot of the IO overhead of chain-requiring a ton of files on initialization. You can still have your setup broken up into multiple chunks if one big blob is too much, but it’s much more performance friendly either way.
- Code bundlers will remove the extraneous stuff that’s often published in NPM packages (readmes, changelogs, test code, multiple dist variants for different runtimes, etc). This alone reduces size by a TON.
- Code bundlers can perform tree shaking to remove dead code that isn’t used.
- Code bundlers can minify the JS code to reduce size further.
- Code bundlers will only load libraries that are actually used in the defined entry point.
The downsides are that you may have some libraries that do very dynamic requires or fs.readFile calls that would require configuration to make sure those assets are still available.
I created a small project to showcase the impact that running a bundle optimizer with tree shaking has for a node project. The project had axios, lodash, and date-fns as main dependencies and a ton of dev dependencies like typescript, eslint, prettier, etc. A minimal webpack config was used.
Using just --production dep reduction
Size of node_modules with both regular and dev deps installed: 149MB
Size of node_modules with --production: 27MB
Size of own codebase: 4KB
Size of build artifacts before zipping: 27MB
Size of build artifacts after zipping: 3.9MB
# Using webpack bundler
Size of build artifact bundled without minification: 256KB
Size of build artifacts bundled/minified: 68KB
Size of build artifacts bundled/minified/zipped: 24KB
Size of source map: 324KB
Size of source map gzipped: 92KB
The deployed zip size goes from 3.9MB to 24KB in this simple example. Note that it’s important to mark the aws-sdk as an external dep so bundlers avoid repackaging it.
However, I’d recommend not using webpack specifically these days. If you’re using the AWS CDK’s NodeJS lambda constructs, they actually already default to bundling using ESBuild out of the box with zero config needed. I’d also consider looking into Vite as an alternative if you’re concerned about how stable/production-ready ESBuild is.
A quick note regarding source maps: You can upload to a service like Sentry and leverage their Lambda extension so you don’t mess with performance of the actual runtime for synchronously invoked lambdas. For async ones, you may be better off with the simpler approach of adding a import 'source-map-support/register' at the top of your entry point.
TL;DR: Bundling is the way to go. ESBuild/Vite are my recommendations, but Webpack works too.