Middy.js

website·Indexed 30 days ago

https://middy.js.org/

A simple middleware engine for Node.js that simplifies AWS Lambda development by decorating main business logic. It provides tools for bundling Lambda packages, connection reuse, internal context management, and profiling, with specific support for API Gateway (HTTP and REST), Alexa Skills Kit, and various middleware such as @middy/http-router and @middy/http-header-normalizer.

Tokens
113.6K
Snippets
416
Records
830
Agent score
73%

What's inside Middy

  1. Overview of Middy.js Pollers

    Middy.js pollers are factory functions exported from subpaths of the package to ensure peer dependencies are only loaded when needed. Each poller factory returns an object containing { source, poll, acknowledge }, which is used to integrate external event sources into a processing loop.
  2. Overview of Middy.js middleware engine

    Middy is a middleware engine for Node.js AWS Lambda functions. It allows developers to separate core business logic from common cross-cutting concerns—such as authentication, authorization, validation, and serialization—by decorating the main handler with modular and reusable middleware, similar to the patterns used in web frameworks like Express.
  3. Overview of AWS Lambda Powertools utilities for Middy.js

    AWS Lambda Powertools provides a set of utilities designed to help build production-ready serverless applications. Several of these utilities are provided as Middy-compatible middleware to automate common tasks like logging, tracing, and validation.
  4. Implement retry semantics for SNS-to-Lambda triggers

    SNS-to-Lambda triggers do not support partial-batch failures; they operate on a fire-and-forget basis per record. To implement robust retry semantics, use a fan-out architecture by routing SNS messages through an SQS queue before triggering the Lambda function (SNS -> SQS -> Lambda).
  5. Decode batch records using event-batch-parser middleware

    The event-batch-parser middleware is a unified body-parser for Lambda batch event sources. It iterates through records from supported sources, performs base64-decoding, optionally strips AWS Glue Schema Registry framing (including decompression), and applies a specified parser (such as JSON, Avro, or Protobuf).

    Supported event sources and their mapped fields:

    • Kafka (Amazon MSK aws:kafka or SelfManagedKafka): Configurable per-field for key and/or value.
    • Kinesis Data Streams (aws:kinesis): Maps data to record.kinesis.data.
    • Kinesis Firehose (aws:lambda:events): Maps data to record.data.
    • SQS (aws:sqs): Maps body to record.body.
    • ActiveMQ (aws:amq): Maps data to message.data.
    • RabbitMQ (aws:rmq): Maps data to message.data.

    Constraint: For non-Kafka sources, you must use the specific field (body or data) that matches the underlying record field. Using the incorrect field will result in a TypeError during startup.

  6. Run Middy handlers as long-running batch consumers on AWS ECS/Fargate

    The @middy/ecs-batch package is a runtime wrapper (not middleware) that enables a Middy handler designed for Lambda batch event source mappings to run on AWS ECS/Fargate. It supports polling from SQS, Kinesis, DynamoDB Streams, Kafka (MSK/Self-Managed), ActiveMQ, and RabbitMQ.

    The runner mimics Lambda's behavior by pulling records, constructing the standard batch event shape, and invoking the handler with (event, context). It uses the handler's return value ({ batchItemFailures: [...] }) to perform native acknowledgments (e.g., DeleteMessageBatch for SQS or channel.ack for RabbitMQ). For stream sources like Kinesis and DynamoDB Streams, the iterator advances implicitly, and the handler is responsible for checkpointing.

  7. Evaluate the performance and architectural cost of using Middy.js

    Adopting Middy.js has minimal impact on Lambda performance and developer experience due to the following characteristics:

    • Bundle Size: @middy/core has no dependencies other than @middy/util. Middlewares are opt-in and support tree-shaking.
    • Cold Starts: The engine initialization takes microseconds. The primary cold start cost is determined by the specific middlewares imported via .use(). To minimize this, tree-shake aggressively and exclude the AWS SDK from the bundle.
    • Runtime Overhead: Each middleware adds exactly one function call before or after the handler; it does not use proxies, decorators, or reflection.
    • Mental Model: The system uses a single primary concept: .use() is used to register middleware with optional before, after, and onError hooks. The handler maintains the standard AWS Lambda signature.
  8. Fetch and cache DynamoDB configuration with the dynamodb middleware

    The dynamodb middleware for Middy.js allows you to fetch configuration values stored in DynamoDB and automatically unmarshal DynamoDB AttributeValues into native JavaScript values, making them easily accessible within your Lambda function.
  9. Fetch and cache S3 configuration using Middy s3 middleware

    The s3 middleware for Middy.js allows a Lambda function to fetch configuration stored in an Amazon S3 bucket and automatically parse it as JSON. This is typically used to manage dynamic configuration without redeploying the Lambda function.
  10. Process Amazon SQS messages with Middy.js

    Use Middy.js to handle Lambda functions triggered by SQS event source mappings. Middy provides capabilities for per-record parsing, executing business logic for individual messages, and reporting partial-batch failures to avoid reprocessing successful messages in a batch.