Skip to content

pavi2410/aws-lambda-nodejs-typescript-monorepo

Repository files navigation

Lambda Functions with NPM Workspaces

This project demonstrates how to use NPM workspaces to organize AWS Lambda functions with shared code packages.

Project Structure

.
├── functions/         # Lambda function entrypoints
│   ├── lambda-1/      # Individual lambda package
│   └── lambda-2/      # Individual lambda package
└── packages/          # Shared code packages
    ├── db/            # Database utilities
    └── routes/        # Routing logic 

Setup & Configuration

The project uses NPM workspaces as defined in package.json:

{
  "workspaces": [
    "functions/lambda-1",
    "functions/lambda-2", 
    "packages/db",
    "packages/routes"
  ]
}

How It Works

  1. Lambda functions are kept in isolated packages under functions/
  2. Shared code lives in separate packages under packages/
  3. Lambda functions can import shared packages using @example/* namespace
  4. 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")
}

Building Lambda Functions

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

Benefits

  • 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

Setup

  1. Install dependencies:
npm install
  1. Build & Run the Lambda function:
./build-function.sh

Requirements

  • Node.js
  • NPM 7+ (for workspaces support)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published