Lightweight Web Framework for AWS Lambda
lambda-api is a zero-dependency, lightweight web framework designed specifically for building serverless applications on AWS Lambda. It supports both AWS API Gateway Lambda Proxy Integration and ALB Lambda Target Support, providing a minimal yet familiar API similar to Express.js or Fastify for routing, middleware, and error handling. Optimized for the stateless, single-run execution model of Lambda, it prioritizes fast cold starts and low memory consumption by eschewing external dependencies. The package is currently at version 1.2.0, with a consistent release cadence of minor versions to introduce new features, enhance TypeScript type definitions, and resolve bugs. Its primary differentiation lies in its minimal footprint and dedicated focus on the unique demands of serverless environments, making it ideal for high-performance, cost-efficient serverless APIs.
Common errors
-
TypeError: Cannot read properties of undefined (reading 's3') OR TypeError: api.s3 is not a function
cause The built-in S3 service functionality is being accessed without the required AWS SDK v3 peer dependencies installed, or the `lambda-api` version is incompatible with the installed AWS SDK version.fixEnsure `lambda-api` is `v1.0.0` or higher, and install `@aws-sdk/client-s3` and `@aws-sdk/s3-request-presigner`. If using `lambda-api@0.12.0` for AWS SDK v2, ensure `aws-sdk` is installed. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an environment configured for ES Modules (e.g., `"type": "module"` in `package.json`).fixChange your import statement to `import lambdaApi from 'lambda-api'; const api = lambdaApi();`. Alternatively, ensure your file is treated as CommonJS (e.g., change file extension to `.cjs` or set `"type": "commonjs"` in `package.json`). -
Error: Cannot find module 'lambda-api' or 'lambda-api/package.json'
cause The `lambda-api` package has not been installed or is not resolvable from the current working directory.fixRun `npm install lambda-api` or `yarn add lambda-api` in your project directory.
Warnings
- breaking Version 1.0.0 migrated to AWS SDK v3. If your application uses `lambda-api`'s S3 service functionality, you must ensure `@aws-sdk/client-s3` and `@aws-sdk/s3-request-presigner` (peer dependencies) are installed and your code is updated for SDK v3 syntax.
- gotcha As of v1.0.1, AWS SDK v3 packages (`@aws-sdk/client-s3`, `@aws-sdk/s3-request-presigner`) are peer dependencies. They are not bundled with `lambda-api` and must be explicitly installed if your application leverages `lambda-api`'s S3 service.
- deprecated Users still relying on AWS SDK v2 should use `lambda-api@0.12.0`. Version 1.x and above are exclusively built for AWS SDK v3.
- security A prototype pollution vulnerability in the `deepMerge` utility was fixed in version 0.11.2. This could potentially allow an attacker to modify properties of `Object.prototype`, affecting all objects in the application.
Install
-
npm install lambda-api -
yarn add lambda-api -
pnpm add lambda-api
Imports
- api
import { api } from 'lambda-api'; // Incorrectly assumes named export const api = new API(); // Incorrectly assumes class constructorimport lambdaApi from 'lambda-api'; const api = lambdaApi();
- API
import { API } from 'lambda-api'; // Incorrectly imports the type as a value import API from 'lambda-api'; // Incorrectly assumes default export is the typeimport type { API } from 'lambda-api'; - CommonJS (API instance)
const lambdaApi = require('lambda-api'); const api = lambdaApi; // Fails to invoke the factory function const { api } = require('lambda-api'); // Incorrectly assumes named exportconst api = require('lambda-api')();
Quickstart
import lambdaApi from 'lambda-api';
const api = lambdaApi();
// Define a route that responds with a status object
api.get('/status', async (req, res) => {
return { status: 'ok', timestamp: new Date().toISOString() };
});
// Define a route that handles a POST request with a body
api.post('/greet', async (req, res) => {
const name = req.body?.name || 'Guest';
return { message: `Hello, ${name}!` };
});
// Declare your Lambda handler function
export const handler = async (event, context) => {
// Run the request through the lambda-api framework
return await api.run(event, context);
};