Skip to main content

How to route events with Amazon EventBridge

Build an event-driven workflow with Amazon EventBridge using custom buses, pattern matching, input transformers, and scheduled rules.

Difficulty
Intermediate
Duration
45 minutes
Steps
6

Amazon EventBridge is a serverless event bus. Producers publish events; rules match them against patterns and route matches to targets like Lambda, Step Functions, or SQS. EventBridge shines for many-to-many routing and integrating SaaS and AWS service events without custom glue code.

Prerequisites

  • An AWS account and Terraform.
  • A target such as a Lambda function.

Steps

1. Create an event bus

The default bus carries AWS service events; create a custom bus for your application events:

resource "aws_cloudwatch_event_bus" "app" { name = "app-bus" }

2. Define an event pattern

Patterns match on event fields:

{ "source": ["orders"], "detail-type": ["OrderCreated"] }

EventBridge routes only events whose fields match every clause.

3. Add a rule and target

resource "aws_cloudwatch_event_rule" "on_order" {
  event_bus_name = aws_cloudwatch_event_bus.app.name
  event_pattern  = jsonencode({ source = ["orders"], "detail-type" = ["OrderCreated"] })
}
resource "aws_cloudwatch_event_target" "to_lambda" {
  rule           = aws_cloudwatch_event_rule.on_order.name
  event_bus_name = aws_cloudwatch_event_bus.app.name
  arn            = aws_lambda_function.handler.arn
}

4. Publish a custom event

aws events put-events --entries '[{"Source":"orders","DetailType":"OrderCreated","Detail":"{\"id\":1}","EventBusName":"app-bus"}]'

5. Transform the input

Use an input transformer to reshape the event before it reaches the target, passing only the fields the target needs.

6. Schedule a rule

EventBridge can fire on a cron or rate schedule, replacing standalone cron jobs:

schedule_expression = "rate(5 minutes)"

Verification

Publish a matching event and confirm the target Lambda runs (check its CloudWatch logs). Publish a non-matching event and confirm nothing fires. For scheduled rules, watch invocations arrive on the expected cadence.

Next Steps

Add a dead-letter queue and retry policy to targets, archive and replay events for debugging, and connect SaaS partner event sources to the bus.

Prerequisites

  • AWS account
  • A Lambda or other target
  • Terraform installed

Steps

  • 1
    Create an event bus
  • 2
    Define an event pattern
  • 3
    Add a rule and target
  • 4
    Publish a custom event
  • 5
    Transform the input
  • 6
    Schedule a rule