iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

EventBridge

EventBridge: AWS event bus. Schedules, custom events, partner integrations, and the patterns for decoupled architectures.

AWS — EventBridge

EXAMPLE
# ===== What EventBridge is =====
# Serverless event bus. Producers publish events; consumers subscribe via RULES.
# Default bus: AWS service events (EC2 state changes, S3 uploads, etc).
# Custom buses: your own events.
# Partner buses: SaaS providers (Auth0, Datadog, Zendesk, ...).

# ===== Create a custom bus =====
aws events create-event-bus --name shop

# ===== Send an event =====
aws events put-events --entries '[{
  "Source": "shop.checkout",
  "DetailType": "OrderPlaced",
  "Detail": "{\"order_id\":\"o-1\",\"customer_id\":\"c-1\",\"total\":4995}",
  "EventBusName": "shop"
}]'

# Detail must be a JSON string (escape internal quotes).

# ===== Create a rule (event pattern) =====
aws events put-rule --name OrderPlacedRule --event-bus-name shop \
  --event-pattern '{ "source": ["shop.checkout"], "detail-type": ["OrderPlaced"] }'

# ===== Add a target =====
aws events put-targets --rule OrderPlacedRule --event-bus-name shop \
  --targets 'Id=1,Arn=arn:aws:lambda:ap-southeast-2:1234:function:onOrderPlaced'

# Then grant invoke permission to EventBridge.

# ===== Common event patterns =====
# All from a source:
{ "source": ["shop.checkout"] }

# Specific detail-type:
{ "detail-type": ["OrderPlaced", "OrderCancelled"] }

# Conditional content:
{
  "source": ["shop.checkout"],
  "detail": {
    "total": [{ "numeric": [">", 100] }],
    "region": ["AU", "NZ"]
  }
}

# ===== Schedules (formerly CloudWatch Events) =====
aws events put-rule --name nightly-cleanup --schedule-expression 'rate(1 day)'
aws events put-rule --name weekly-report --schedule-expression 'cron(0 4 ? * MON *)'

# Modern: use EventBridge Scheduler (separate service, more features):
aws scheduler create-schedule --name nightly --schedule-expression 'rate(1 day)' \
  --target '{"Arn":"...","RoleArn":"..."}'

# ===== Targets =====
# Lambda, SQS, SNS, Step Functions, ECS task, Kinesis, API destinations (any HTTP),
# Pipes (event router with transformation), EventBus (cross-account / region).

# ===== Pipes =====
# Connect a source (SQS, Kinesis, DynamoDB stream, MQ) -> filter -> enrich -> target.
# Replaces a Lambda glue function for many ingestion patterns.

# ===== API destinations =====
# Targets that POST to an HTTP endpoint (your own service, SaaS API).
# Built-in retries + connection auth (API key, OAuth).

# ===== Dead-letter queues =====
# Per-target DLQ on EventBridge rules; failed deliveries land in SQS.
- DeadLetterConfig: { Arn: 'arn:aws:sqs:...:dlq' }

# ===== Cost =====
# Default bus: free for AWS service events
# Custom + partner buses: USD 1 per million events
# Replays + cross-region forwarding: extra
# Compared to SNS/SQS, similar order of magnitude.

# ===== When EventBridge wins =====
# - Loosely-coupled microservices (event-driven)
# - SaaS integrations (partner buses)
# - Scheduled jobs (replaces cron servers)
# - Audit + replay (archive + replay events)

# ===== Patterns =====
# - One bus per business domain (orders, payments, support)
# - Detail-type as the 'event name'; source as the producing service
# - DLQ on every rule
# - Archive + replay for debugging + DR

# ===== Pitfalls =====
# - Catch-all rules that fan out to many targets -> coupling re-emerges
# - PutEvents batches limited to 10; chunk producer loops
# - JSON Detail must be a string; double-encoding bugs are common
# - No ordering guarantees within a bus; design for idempotent consumers

Why it matters

EventBridge is the AWS event bus: producers put events, rules + targets fan them out. Patterns on source + detail-type + detail filter cleanly. Pair with Pipes for sourcing, API destinations for HTTP webhooks, Scheduler for cron, and DLQs for resilience. Designed for loosely-coupled domains.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Event bus — schedule jobs + route AWS service events to handlers.
Try it Yourself »

Discussion

Loading…