Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsJul 24, 2026·3 min de lectura

Middy — Middleware Engine for AWS Lambda in Node.js

A lightweight middleware framework for AWS Lambda that simplifies input parsing, validation, error handling, and other cross-cutting concerns.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Middy Lambda Middleware
Comando de instalación directa
npx -y tokrepo@latest install c9146a67-86f9-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

Introduction

Middy is a middleware engine for AWS Lambda functions in Node.js. It applies the onion model to Lambda handlers, letting developers compose reusable layers for parsing, validation, caching, error handling, and other cross-cutting concerns. This keeps business logic clean while moving boilerplate into composable middleware.

What Middy Does

  • Wraps Lambda handlers with a before/after/onError middleware chain
  • Provides official middleware for common tasks: JSON parsing, validation, CORS, SSM parameter fetching, and more
  • Normalizes event and response objects across AWS event sources
  • Catches errors and formats them into structured HTTP responses
  • Supports ESM and CommonJS module formats

Architecture Overview

Middy wraps a Lambda handler and maintains an ordered list of middleware objects. Each middleware can define before, after, and onError hooks. On invocation, Middy runs before hooks in order, then the handler, then after hooks in reverse. If any step throws, onError hooks run in reverse. The chain fully supports async/await with minimal cold-start overhead.

Self-Hosting & Configuration

  • Install via npm: npm install @middy/core plus desired middleware packages
  • No server to manage — it runs inside your Lambda function
  • Configure middleware by passing options to each middleware factory function
  • Compatible with AWS SAM, Serverless Framework, CDK, SST, and other deploy tools
  • Works with Node.js 18+ in both ESM and CommonJS

Key Features

  • Onion-model middleware with before, after, and onError lifecycle hooks
  • Over 30 official middleware packages covering parsing, validation, caching, and secrets
  • TypeScript support with full type definitions
  • Minimal cold-start overhead with lazy-loading capabilities
  • Simple API for writing custom middleware

Comparison with Similar Tools

  • Lambda Powertools — AWS-maintained utilities for logging, tracing, and metrics; Middy focuses on the middleware pattern
  • Serverless Express — Adapts Express apps to Lambda; Middy is Lambda-native without HTTP framework overhead
  • DAZN Lambda Powertools — Opinionated middleware set; Middy provides a flexible engine with a la carte middleware
  • Hono — Web framework that runs on Lambda; Middy wraps raw Lambda handlers without routing assumptions

FAQ

Q: Does Middy add significant cold-start latency? A: Middy's core is lightweight. Cold-start impact depends on which middleware you attach; each is a separate package to keep bundles small.

Q: Can I write my own middleware? A: Yes. A middleware is an object with optional before, after, and onError async functions. No special registration is needed.

Q: Does Middy work with TypeScript? A: Yes. Middy and all official middleware packages ship with TypeScript type definitions.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados