EventBridge buses, rules and Scheduler

Amazon Web Services · Practical · 14 min

EventBridge is the hub of event-driven architectures on AWS and the standard way to react to things happening in your account.

Amazon EventBridge routes events, which are JSON documents with fields like source, detail-type and detail. Events arrive on an event bus:

  • The default bus receives events from AWS services (an EC2 state change, an ECS task stopping, a CodePipeline failure).
  • Custom buses carry your application's domain events, published with PutEvents.
  • Partner buses receive events from SaaS providers.

Rules match events with event patterns and send them to up to five targets: Lambda, SQS, SNS, Step Functions, Kinesis, ECS tasks, another bus (also cross-account or cross-Region), or any HTTPS endpoint via API destinations. Patterns match exact values, prefixes, numeric ranges, exists, and anything-but, and input transformers reshape the payload for the target.

Delivery is at least once, retried for up to 24 hours by default; attach a DLQ to a target to keep events that still fail. Archive and replay lets you re-send past events, useful after fixing a consumer bug.

Related features:

  • EventBridge Scheduler: one-off or recurring (cron, rate) invocations of thousands of targets, with time zones and flexible windows. It replaces scheduled rules for new work.
  • EventBridge Pipes: point-to-point integration from a source (SQS, Kinesis, DynamoDB Streams) through filtering and enrichment to a target, without glue code.

Compared with SNS, EventBridge offers richer content filtering, more target types, schemas and replay; SNS offers higher fan-out throughput and push to mobile, SMS and email.

Example

import json, boto3

ev = boto3.client("events", region_name="eu-west-1")
sqs = boto3.client("sqs", region_name="eu-west-1")
q = sqs.create_queue(QueueName="big-orders")["QueueUrl"]
q_arn = sqs.get_queue_attributes(QueueUrl=q, AttributeNames=["QueueArn"])["Attributes"]["QueueArn"]

ev.create_event_bus(Name="shop")
ev.put_rule(Name="big-orders", EventBusName="shop", EventPattern=json.dumps({
    "source": ["shop.orders"], "detail-type": ["OrderPlaced"],
    "detail": {"total": [{"numeric": [">=", 100]}]}}))
ev.put_targets(Rule="big-orders", EventBusName="shop", Targets=[{"Id": "q", "Arn": q_arn}])

for total in (40, 250):
    ev.put_events(Entries=[{"EventBusName": "shop", "Source": "shop.orders",
        "DetailType": "OrderPlaced", "Detail": json.dumps({"total": total})}])

msgs = sqs.receive_message(QueueUrl=q, MaxNumberOfMessages=10).get("Messages", [])
print([json.loads(m["Body"])["detail"]["total"] for m in msgs])   # [250]

Remember

  • Events are JSON with source, detail-type and detail
  • Rules match patterns (including numeric and prefix matches) and route to targets
  • Use DLQs on targets and archive/replay for recovery
  • EventBridge Scheduler handles cron and one-off schedules

Watch out for

  • Publishing events with inconsistent source or detail-type naming, so rules silently match nothing.

Check yourself

  1. You want a Lambda to run whenever any ECS task in the account stops. Where does the event come from?
  2. A rule's pattern is {"detail": {"total": [{"numeric": [">=", 100]}]}}. Which event matches?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html

Builds on

Unlocks

Nothing builds on this yet.