Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsJul 24, 2026·3 min de lecture

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.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Middy Lambda Middleware
Commande d'installation directe
npx -y tokrepo@latest install c9146a67-86f9-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en 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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires