Packaging Lambda Code

Bundle only what the function needs — small packages mean faster cold starts and deploys.

How you package a Lambda affects its speed and size. There are three options.

Packaging choices

  • Zip — bundle code and dependencies into a zip (up to 250 MB unzipped).
  • Container image — package as a Docker image (up to 10 GB) stored in ECR, ideal for large dependencies.
  • Layers — share common dependencies across functions without repackaging them each time.

Keep it small

Strip dev dependencies, tree-shake, and exclude test files. A smaller package downloads and initializes faster, which directly shrinks cold-start latency.

Example

Example · bash
# Bundle a Node Lambda to one small file with esbuild
npx esbuild src/index.ts \
  --bundle --minify --platform=node --target=node20 \
  --outfile=dist/index.js

cd dist && zip fn.zip index.js
aws lambda update-function-code --function-name myapp-api --zip-file fileb://fn.zip

When to use it

  • A team adds only production dependencies to the Lambda zip, keeping it under 10 MB so cold starts are noticeably faster.
  • A Python Lambda uses a Lambda Layer for numpy and scipy so the function zip stays small and those heavy libraries are shared across functions.
  • A CI pipeline builds and uploads a Lambda zip to S3, then updates the function code by referencing the S3 key rather than uploading directly.

More examples

Package Node.js Lambda without devDeps

Copies source and installs only production dependencies before zipping, excluding test files to minimise package size.

Example · bash
mkdir -p dist
cp -r src dist/
cp package*.json dist/
cd dist && npm ci --omit=dev
zip -r ../function.zip . -x '*.test.js'
echo "Package size: $(du -sh ../function.zip | cut -f1)"

Upload large package via S3

Uploads the zip to S3 first and deploys by S3 reference, which is required for packages larger than 50 MB.

Example · bash
aws s3 cp function.zip s3://my-deploy-bucket/lambda/function-${GIT_SHA}.zip

aws lambda update-function-code \
  --function-name my-handler \
  --s3-bucket my-deploy-bucket \
  --s3-key lambda/function-${GIT_SHA}.zip

Publish a Lambda Layer for shared deps

Packages shared npm dependencies as a Lambda Layer so multiple functions can reference it without duplicating the code.

Example · bash
mkdir -p layer/nodejs
cd layer/nodejs && npm install axios lodash
cd .. && zip -r ../shared-layer.zip nodejs/

aws lambda publish-layer-version \
  --layer-name shared-utils \
  --zip-file fileb://../shared-layer.zip \
  --compatible-runtimes nodejs20.x

Discussion

  • Be the first to comment on this lesson.
Packaging Lambda Code — AWS Deployment | SoundsCode