Middy Middleware Engine for Node.js AWS Lambda
repository·main·Indexed 26 days ago
https://github.com/middyjs/middyA middleware engine for Node.js AWS Lambda functions designed to separate business logic from cross-cutting concerns such as validation, authentication, and error handling. It provides a wide array of specialized middleware packages including @middy/core, @middy/http-cors, @middy/dynamodb, @middy/http-jwt, and @middy/event-normalizer.
What's inside Middy
- Middy is a middleware engine for AWS Lambda using Node.js. It allows you to separate common technical concerns—such as authentication, authorization, validation, serialization, and input parsing—from your core business logic. By using the middleware pattern (similar to Express), you can decorate your Lambda handler with modular, reusable pieces of logic, keeping your main handler clean and focused on business requirements.
Overview of @middy/event-normalizer
mainThe@middy/event-normalizermiddleware provides AWS event parsing and normalization for the Middy framework. It is designed to simplify handling various AWS Lambda event types by providing a consistent, normalized structure for your application logic.Overview of Middy features
mainMiddy is a middleware engine for Node.js AWS Lambda functions. It helps separate non-functional concerns (like validation, auth, and logging) from your core business logic.
Key Features:
- Extensive Ecosystem: 52 official packages for services like API Gateway, SQS, S3, DynamoDB, SNS, EventBridge, Kinesis, Kafka, and WebSockets.
- Modern Runtime Support: Built with TypeScript types, supports Node.js >= 22, and uses ESM.
- Lightweight Core: The
@middy/corepackage is tiny and does not include the AWS SDK. - Advanced Routing: Includes routers for HTTP, WebSocket, and CloudFormation custom resources.
- Lambda Optimizations: First-class support for AWS Lambda response streaming and durable functions.
Overview of official Middy middlewares
mainMiddy provides a suite of official, opt-in plugins maintained by the core team. These middlewares are designed to be performant, single-task focused, and kept in sync with the core package. They address common use cases when using AWS Lambda with other AWS services, categorized into Observability, Lifecycle, Request Transformation, Response Transformation, Fetch Data, and Lambda Extensions.Review Middy's pipeline hardening and security conventions
mainMiddy employs several hardening techniques across all CI/CD workflows:
- Pinned Actions: All third-party GitHub Actions are pinned to specific commit SHAs.
- Scoped Permissions: Workflows use minimal required permissions (e.g.,
contents: readby default). - Runner Hardening: Every job utilizes
step-security/harden-runnerinauditmode to monitor egress. - Credential Management: No long-lived credentials are used;
npm publishutilizes OIDC, and theGITHUB_TOKENauto-rotates per job. - Ephemeral Runners: All jobs run on GitHub-hosted
ubuntu-latestrunners. - Workflow Security: Workflows are linted using
zizmorandactionlintto prevent misconfigurations.
Integrate Powertools for AWS Lambda with Middy
mainPowertools for AWS is a developer toolkit for implementing serverless best practices. It is compatible with
@middy/coreversions 4.x through 7.x.Compatible utilities include:
- Logger: Structured JSON logging with Lambda context and cold start capture.
- Tracer: AWS X-Ray wrapper for tracing invocations, HTTP requests, and AWS SDK calls.
- Metrics: Asynchronous CloudWatch custom metrics using EMF format.
- Idempotency: Prevents duplicate execution based on payload content.
- Parser: Data validation and parsing using Zod.
Use @middy/event-batch-response to shape batch responses
mainThe@middy/event-batch-responsemiddleware is used to automatically shape the Lambda response for batch-based event sources. This ensures that the response format is compatible with the requirements of services like SQS, Kinesis, or DynamoDB Streams, allowing for partial batch failures or successful processing acknowledgments.Use @middy/dynamodb middleware
mainThe
@middy/dynamodbmiddleware provides helper functionality for interacting with Amazon DynamoDB within your Middy-powered AWS Lambda functions.For detailed documentation, configuration options, and usage examples, please refer to the official Middy documentation at https://middy.js.org/docs/middlewares/dynamodb.
Use @middy/http-paseto middleware
mainThe@middy/http-pasetomiddleware provides HTTP PASETO authentication for AWS Lambda functions using the Middy engine. For detailed documentation, configuration options, and usage examples, visit the official Middy documentation website.Use @middy/cloudformation-response for CloudFormation Custom Responses
mainThe@middy/cloudformation-responsemiddleware handles event response formatting for AWS CloudFormation Custom Resources. It ensures that the Lambda response follows the required structure for CloudFormation to correctly process the resource lifecycle (e.g.,SUCCESSorFAILEDstatuses).Middy CI/CD Security and Compliance Overview
mainMiddy follows the OWASP SPVS 1.5 standard for its CI/CD pipeline. The security posture is divided into five main phases:
- Integrate (CI): Focuses on pipeline environment hardening (using
step-security/harden-runner), credential hygiene (GitHub Secrets and OIDC), and continuous security checks (SAST, SCA, and DAST). - Release (CD): Ensures final security assessments via full PR gating, automated deployment via
release.yml, and artifact integrity through Sigstore provenance. - Operate: Includes access audits, continuous enforcement of security standards (OSSF Scorecard), and timely patching via Dependabot.
- Detection and Monitoring: Utilizes GitHub Actions logs and egress audits to detect anomalies.
- Incident Response: Maintains documented plans for triage, containment, and recovery (e.g., secret rotation and patched-version republishing).
- Integrate (CI): Focuses on pipeline environment hardening (using
Review OWASP SPVS 1.5 Compliance for Middy
mainMiddy maintains compliance with OWASP SPVS 1.5 standards across several development domains, including secure coding, software quality, code review, security checks, credential hygiene, and third-party library auditing. This document serves as a record of the requirements met and the evidence provided (e.g., GitHub Actions workflows, linting rules, and security scanning tools).