Skip to content

pyepye/lambda-event-router

Repository files navigation

Lambda Event Router logo

Lambda Event Router

A TypeScript framework for routing AWS Lambda events. You define routers for the AWS services you care about - SQS, API Gateway, DynamoDB Streams, S3 and more - and the framework works out which router should handle each event.

This is useful when you want a single Lambda (or a set of Lambdas with shared code) to handle events from multiple sources. Instead of writing your own event detection logic, you declare filters and handlers and let the router do the matching.


import { LambdaRouter } from '@lambda-event-router/base'
import { apiGatewayRouter } from './api'
import { sqsRouter } from './sqs'
import { dynamoDBRouter } from './dynamo'
import { s3Router } from './s3'

const lambdaRouter = new LambdaRouter({
  routers: [apiGatewayRouter, sqsRouter, dynamoDBRouter, s3Router]
})

export const handler = lambdaRouter.handler()

Features

  • Multi-source routing - Combine routers from different AWS services in a single Lambda handler
  • Type-safe - Full TypeScript support with inferred types from schemas and filters for inline handlers
  • Declarative filters - Route events by service-specific data - ARN, eventType, topic, bucket, event name, detail type and custom filter functions
  • Native support for 29+ AWS services - Includes dedicated routers for SQS, SNS, EventBridge, DynamoDB Streams, S3, API Gateway, and more. Any service that emits Lambda events is supported out of the box.
  • Works with any AWS service - Even services without native Lambda support can be integrated using CloudTrail and EventBridge using the EventBridgeRouter.
  • Schema validation - Built-in validation for request bodies, message attributes, path params and more. Works with any Standard Schema library
  • Well tested - Clear tests covering most code branches for each event type

This framework isn't the right fit in every situation. See When not to use it for more details.

Packages / Supported AWS services

Below is an overview of the packages and routers included. Each package can be installed individually.

For usage details, check the individual READMEs linked in the table.

Service Feature / Event Source Package Router Link
Amazon API Gateway REST API @lambda-event-router/apigateway APIGatewayRouter README
Amazon API Gateway HTTP API @lambda-event-router/apigateway APIGatewayRouter README
Amazon API Gateway WebSocket @lambda-event-router/apigateway WebSocketRouter README
Amazon API Gateway Lambda Authorizer @lambda-event-router/apigateway LambdaAuthorizerRouter README
Elastic Load Balancing Application Load Balancer @lambda-event-router/alb ALBRouter README
Amazon VPC Lattice VPC Lattice @lambda-event-router/vpclattice VPCLatticeRouter README
Amazon SQS Queue @lambda-event-router/sqs SQSRouter README
Amazon SNS Topic notification @lambda-event-router/sns SNSRouter README
Amazon DynamoDB Streams @lambda-event-router/dynamodb DynamoDBRouter README
Amazon Kinesis Data Streams @lambda-event-router/kinesis KinesisRouter README
Amazon Data Firehose Transformation @lambda-event-router/firehose FirehoseRouter README
Amazon MSK / Self-managed Kafka @lambda-event-router/kafka KafkaRouter README
Amazon MQ ActiveMQ @lambda-event-router/mq ActiveMQRouter README
Amazon MQ RabbitMQ @lambda-event-router/mq RabbitMQRouter README
Amazon DocumentDB Change Streams @lambda-event-router/documentdb DocumentDBRouter README
Amazon EventBridge Rule @lambda-event-router/eventbridge EventBridgeRouter README
Amazon EventBridge Pipe @lambda-event-router/eventbridge EventBridgeRouter README
Amazon EventBridge Bus @lambda-event-router/eventbridge EventBridgeRouter README
Amazon EventBridge Scheduler @lambda-event-router/base EventRouter README
Amazon S3 Object notification @lambda-event-router/s3 S3Router README
Amazon S3 Batch Operations @lambda-event-router/s3 S3Router README
Amazon CloudWatch Logs subscription filter @lambda-event-router/cloudwatch CloudWatchLogsRouter README
AWS Step Functions Task @lambda-event-router/stepfunctions StepFunctionsRouter README
AWS CodePipeline Job @lambda-event-router/codepipeline CodePipelineRouter README
Amazon Cognito User Pool triggers @lambda-event-router/cognito CognitoRouter README
AWS AppSync Resolver @lambda-event-router/appsync AppSyncRouter README
AWS AppSync Authorizer @lambda-event-router/appsync AppSyncAuthorizerRouter README
AWS AppSync Events @lambda-event-router/appsync AppSyncEventsRouter README
Amazon SES Email receipt @lambda-event-router/ses SESRouter README
Amazon Connect Contact flow @lambda-event-router/connect ConnectRouter README
Amazon Lex Bot (v2) @lambda-event-router/lex LexRouter README
AWS Secrets Manager Rotation @lambda-event-router/secretsmanager SecretsManagerRouter README
AWS Config Custom Rule @lambda-event-router/config ConfigRouter README
AWS Config Scheduled Rule @lambda-event-router/config ConfigScheduledRouter README
AWS CodeCommit Repository trigger @lambda-event-router/codecommit CodeCommitRouter README
AWS CloudFormation Custom Resource @lambda-event-router/cloudformation CloudFormationRouter README
AWS IoT Core Rules Engine action @lambda-event-router/iot IoTRouter README

Quick start

Install the base package and the service router you need:

npm install @lambda-event-router/[package]

For example, to install routers for apigateway and sqs

npm install @lambda-event-router/apigateway @lambda-event-router/sqs

Basic usage

Here's a quick look at how you define handlers. For the full details on each router, check their individual READMEs.

// main handler
import { LambdaRouter } from '@lambda-event-router/base'
import { apiRouter } from './api'
import { sqsRouter } from './sqs'

const lambdaRouter = new LambdaRouter({
  routers: [apiRouter, sqsRouter]
})

export const handler = lambdaRouter.handler()
// api.ts
import { createAPIGatewayRouter } from '@lambda-event-router/apigateway'

const apiRouter = createAPIGatewayRouter()

apiRouter.route({
  filter {
    method: 'POST'
    path: '/order/:id/'
  },
  handler: createOrder // Puts message on an SQS queue with a message attribute of ProcessOrder (string)
})
// sqs.ts
import { createSQSRouter } from '@lambda-event-router/sqs'
import { processOrder } from './order/process'
import { refundOrder } from './order/refund'

const sqsRouter = createSQSRouter()

sqsRouter.route({
  filter: {
    messageAttributes: {
      Type: ['ProcessOrder'],
    }
  },
  handler: processOrder
})

sqsRouter.route({
  filter: {
    messageAttributes: {
      Type: ['RefundOrder'],
    }
  },
  handler: refundOrder
})

Examples

See the examples directory for working examples covering every supported service.

There is also a full-examples directory with deployable code you can spin up on your own AWS environment with a few commands.

When not to use it

If your Lambda handles a single event source in a single way, you probably don't need this. Here are some cases where it isn't the right fit:

  • Single event source with no filtering - Your Lambda receives events from one source and processes them all the same way. A plain handler function is simpler
  • Single-purpose Lambdas - Your Lambda does exactly one thing. Routing and filtering add indirection with no upside
  • HTTP-only Lambdas - Dedicated HTTP frameworks like Express, Hono and Fastify have richer ecosystems for middleware, auth and templating
  • Performance-critical Lambdas with simple logic - The router iterates through registered routers via canHandleEvent checks and applies middleware chains. For ultra-simple Lambdas where every millisecond counts, a direct handler avoids this overhead
  • One Lambda per event source - If you intentionally map one Lambda to one event source for isolated scaling, permissions or deployment, there's nothing to route between

About

A TypeScript framework for routing AWS Lambda events

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors