Skip to content

Repository files navigation

Lambda DevKit

npm versionLicenseBuild Status

A development toolkit for compiling and running AWS Lambda functions locally with API Gateway simulation.


Features

  • Local Execution: Run your AWS Lambda functions locally without deploying to AWS.
  • API Gateway Simulation: Simulate API Gateway routes and methods (e.g., GET, POST, PUT, DELETE).
  • Compilation Support: Automatically compile TypeScript or JavaScript Lambda functions.
  • Flexible Configuration: Define routes, handlers, and environment variables in a simple configuration file.
  • Developer-Friendly CLI: Use the ldk command to manage and test your Lambda functions.

Installation

Install the package as a project dependency:

pnpm add @nexckycort/lambda-devkit -D

Quick Start

  1. Create a Configuration File
    Create a file named lambda-devkit.config.ts in the root of your project. Here's an example configuration:
importtype{LambdaDevkitConfig}from'@nexckycort/lambda-devkit';constconfig: LambdaDevkitConfig={server: {routes: [{method: 'GET',path: 'users',lambda: {handler: 'functions/getUsers',timeout: 5000,},},{method: 'POST',path: 'users',lambda: {handler: 'functions/createUser',timeout: 10000,},},],port: 4000,environment: {ENV: 'dev',},},build: {bundle: true,minify: true,external: ['@aws-sdk/*'],},};exportdefaultconfig;
  1. Start the Local Server
    Run the following command to start the local server:

    ldk dev
  2. Test Your Endpoints
    Use tools like Postman or curl to test your endpoints:

    curl http://localhost:4000/users

Usage

Commands

The ldk CLI provides the following commands:

CommandDescription
ldk devStart the local server with API Gateway simulation.
ldk buildCompile your Lambda functions.

Example

Here’s an example of how to use the CLI:

# Build your Lambda functions
ldk build
# Start the local server
ldk dev

Configuration

Lambda DevKit uses a configuration file (lambda-devkit.config.ts) to define routes, handlers, and other settings. Below is a detailed explanation of each field:

server

Server configuration for local development.

FieldTypeDescription
routesRouteConfig[]List of routes and their configurations.
portnumberPort on which the local server will run. Default: 4000.
environmentobjectEnvironment variables to be passed to the Lambda function.

routes

An array of route configurations. Each route maps an HTTP method and path to a Lambda function.

FieldTypeDescription
methodstringHTTP method (e.g., GET, POST, PUT, DELETE).
pathstringPath for the route. Supports wildcards like users/*.
lambdaLambdaConfigConfiguration for the Lambda function that handles requests to this route.

lambda

Configuration for a Lambda function.

FieldTypeDescription
handlerstringPath to the folder containing the Lambda handler (e.g., functions/myFunction).
timeoutnumberMaximum execution time for the Lambda function in milliseconds.

build

Build configuration for the Lambda functions.

FieldTypeDescription
entryPointstringEntry point for the build process. Default: "functions/**".
bundlebooleanWhether to bundle the Lambda function code into a single file.
minifybooleanWhether to minify the Lambda function code during the build process.
sourcemapboolean | stringSourcemap generation options (true, 'linked', 'inline', etc.).
outbasestringBase directory for resolving entry points. Default: "functions".
externalstring[]External libraries that should be excluded from the build. Default: ["@aws-sdk/*"].

Examples

Basic Example

Here’s a simple example of a Lambda function and its configuration:

src/getUsers/index.ts

exportconsthandler=async(event: any)=>{console.log("Event:",event);return{statusCode: 200,body: JSON.stringify([{id: 1,name: "Alice"},{id: 2,name: "Bob"},]),};};

lambda-devkit.config.ts

importtype{LambdaDevkitConfig}from'@nexckycort/lambda-devkit';constconfig: LambdaDevkitConfig={server: {routes: [{method: 'GET',path: 'users',lambda: {handler: 'functions/getUsers'},},],port: 4000,},build: {bundle: true,minify: true,external: ['@aws-sdk/*'],},};exportdefaultconfig;

Run the server:

ldk dev

Test the endpoint:

curl http://localhost:4000/users

License

This project is licensed under the MIT License. See the LICENSE file for details.