This project demonstrates how to use NPM workspaces to organize AWS Lambda functions with shared code packages.
.
├── functions/ # Lambda function entrypoints
│ ├── lambda-1/ # Individual lambda package
│ └── lambda-2/ # Individual lambda package
└── packages/ # Shared code packages
├── db/ # Database utilities
└── routes/ # Routing logic
The project uses NPM workspaces as defined in package.json:
{
"workspaces": [
"functions/lambda-1",
"functions/lambda-2",
"packages/db",
"packages/routes"
]
}
- Lambda functions are kept in isolated packages under
functions/
- Shared code lives in separate packages under
packages/
- Lambda functions can import shared packages using
@example/*
namespace - Each Lambda function and package is a standalone NPM module
Example Lambda using shared code from functions/lambda-1/index.js:
import {route} from "@example/routes"
export function handler() {
return route("/test")
}
Use the build-function.sh script to bundle a Lambda function:
./build-function.sh
This uses esbuild to:
- Bundle the Lambda with dependencies
- Minify the output
- Generate production-ready
bundle.js
- Clean separation between Lambda entry points and business logic
- Shared code is maintainable in dedicated packages
- Consistent versioning across packages
- Single
npm install
for all dependencies - Efficient bundling for deployment
- Install dependencies:
npm install
- Build & Run the Lambda function:
./build-function.sh
- Node.js
- NPM 7+ (for workspaces support)