Overview and What You Will Learn
Amazon EventBridge is a serverless event bus: services publish events, rules match them against patterns, and matching events are delivered to targets such as Lambda, SQS, SNS or Step Functions. The publisher never knows who is listening, so adding a new consumer never means changing the publisher.
In this lab, you will:
- Create an SNS topic and allow EventBridge to publish to it
- Test an event pattern offline before deploying it
- Create a custom event bus with a rule and publish a real event
- Add a schedule rule and an event archive
- Clean everything up in dependency order
Why This Matters in Production
Picture a trading platform like Zerodha. When a trade executes, a dozen downstream systems need to know: the ledger, tax records, notifications, risk checks and the audit log. If the trade service calls each one directly, any slow or failing call slows or fails the trade confirmation. With EventBridge the trade service publishes one event and every system subscribes independently. The same pattern powers security automation, such as alerting the security team when the root user signs in.
Core Principles
How Events Flow Through EventBridge
A producer puts an event on a bus, each rule on that bus evaluates it against its pattern, and every matching rule sends it to its targets. One rule can have up to 5 targets.
pattern} B --> R2{Rule 2
pattern} R1 -->|Match| T1[Lambda] R2 -->|Match| T2[SQS queue] R2 -->|Match| T3[SNS topic] classDef start fill:#475569,color:#FFFFFF classDef bus fill:#F97316,color:#FFFFFF classDef rule fill:#F59E0B,color:#111827 classDef target fill:#326CE5,color:#FFFFFF class P start class B bus class R1,R2 rule class T1,T2,T3 target
Which Event Bus Should You Use
EventBridge has three kinds of bus, and keeping each kind of event on the right one keeps rules simple.
| Bus | Receives |
|---|---|
| Default | AWS service events, such as EC2 state changes and CloudTrail API calls |
| Partner | Events from SaaS providers such as GitHub, Stripe and Datadog |
| Custom | Events your own applications publish |
Common MistakePutting your application events on the Default bus. It exists for AWS service events, and mixing the two makes filtering harder and adds noise. Create a Custom bus for your own events.
Schedule Rules and Event Pattern Rules
A schedule rule runs a target on a timer, like a managed cron job, using rate(5 minutes) or cron(0 2 * * ? *). An event pattern rule reacts when something happens. For new schedules, EventBridge Scheduler is the more capable service, with time zones, one-time schedules and higher scale, while rules with schedule expressions still work.
How to Write an Event Pattern
An event pattern is a JSON filter, and an event matches only if every field you list matches. Match EC2 terminations:
{ "source": ["aws.ec2"], "detail-type": ["EC2 Instance State-change Notification"], "detail": { "state": ["terminated"] }}Match root user console sign-ins, the classic security alert:
{ "source": ["aws.signin"], "detail-type": ["AWS Console Sign In via CloudTrail"], "detail": { "userIdentity": { "type": ["Root"] } }}Match S3 uploads under a prefix. The bucket must have EventBridge notifications turned on:
{ "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["devops-uploads-prod"] }, "object": { "key": [{ "prefix": "invoices/" }] } }}Patterns also support suffix, anything-but, numeric ranges and exists checks.
RememberConsole sign-in events for the root user are often delivered in us-east-1, so create a root login rule in that Region and confirm where your sign-in events appear.
Schema Registry, Pipes, and Archives
Schema Registry discovers the structure of events on a bus and generates typed code bindings for Python, Java, TypeScript and Go, so your consumer already knows detail.orderId is a string. EventBridge Pipes connect a source (SQS, Kinesis, DynamoDB Streams, Kafka) to a target with optional filtering and enrichment, replacing a Lambda that only moves data. An archive stores events from a bus so you can replay them later.
optional Lambda] E --> T[SQS queue] classDef src fill:#475569,color:#FFFFFF classDef step fill:#F97316,color:#FFFFFF classDef tgt fill:#326CE5,color:#FFFFFF class S src class F,E step class T tgt
TipTurn on archiving from day one. You can only replay events that were archived, and replay is the fastest way to test a new rule against real traffic or recover after a consumer outage.
Cross-Account and Cross-Region Routing
A rule can target another bus in a different account or Region. Security teams use this to collect events from every account on one central bus, and DR designs use it to copy events to a standby Region.
Detailed Step-by-Step Practical Lab
Replace the email address with your own. The lab uses the AWS CLI, creates no always-on compute, and costs almost nothing.
- Create an SNS topic and subscribe your email. Confirm the subscription from your inbox before continuing:
export AWS_REGION=ap-south-1 export TOPIC_ARN=$(aws sns create-topic --name devops-event-alerts \ --query TopicArn --output text --region $AWS_REGION) aws sns subscribe --topic-arn $TOPIC_ARN \ --protocol email --notification-endpoint rahul@devops.in --region $AWS_REGION- Allow EventBridge to publish to the topic. Without this policy, rules created from the CLI deliver nothing:
cat > sns-policy.json << EOF{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sns:Publish", "Resource": "$TOPIC_ARN" }]}EOF aws sns set-topic-attributes --topic-arn $TOPIC_ARN \ --attribute-name Policy --attribute-value file://sns-policy.json --region $AWS_REGION- Test the pattern offline.
test-event-patternreturnstruewhen an event matches, so you catch mistakes before deploying:
cat > order-pattern.json << 'EOF'{ "source": ["devops.orders"], "detail-type": ["OrderPlaced"]}EOF cat > sample-event.json << 'EOF'{ "id": "11111111-2222-3333-4444-555555555555", "version": "0", "account": "123456789012", "time": "2026-10-14T09:00:00Z", "region": "ap-south-1", "resources": [], "source": "devops.orders", "detail-type": "OrderPlaced", "detail": { "orderId": "ORD-001", "city": "Mumbai", "amount": 450 }}EOF aws events test-event-pattern \ --event-pattern file://order-pattern.json \ --event file://sample-event.json --region $AWS_REGION- Create a custom bus, then a rule and a target on it:
aws events create-event-bus --name mumbai-orders-bus --region $AWS_REGION aws events put-rule --name new-order-processor \ --event-bus-name mumbai-orders-bus \ --event-pattern file://order-pattern.json --region $AWS_REGION aws events put-targets --rule new-order-processor \ --event-bus-name mumbai-orders-bus \ --targets "Id"="notify-team","Arn"="$TOPIC_ARN" --region $AWS_REGION- Publish a custom event. The output should show
FailedEntryCount: 0, and an email arrives within a minute:
aws events put-events --region $AWS_REGION --entries '[{ "EventBusName": "mumbai-orders-bus", "Source": "devops.orders", "DetailType": "OrderPlaced", "Detail": "{\"orderId\": \"ORD-002\", \"city\": \"Mumbai\", \"amount\": 450}"}]'- Add a schedule rule on the default bus that fires every 5 minutes, then check it:
aws events put-rule --name devops-health-check \ --schedule-expression "rate(5 minutes)" --region $AWS_REGION aws events put-targets --rule devops-health-check \ --targets "Id"="ping-team","Arn"="$TOPIC_ARN" --region $AWS_REGION aws events describe-rule --name devops-health-check --region $AWS_REGION- Archive the custom bus so its events can be replayed later:
export BUS_ARN=$(aws events describe-event-bus --name mumbai-orders-bus \ --query Arn --output text --region $AWS_REGION) aws events create-archive --archive-name mumbai-orders-archive \ --event-source-arn $BUS_ARN --retention-days 7 --region $AWS_REGION- Clean up. Remove targets before deleting rules, and rules before deleting the bus:
aws events remove-targets --rule devops-health-check --ids ping-team --region $AWS_REGIONaws events delete-rule --name devops-health-check --region $AWS_REGION aws events remove-targets --rule new-order-processor \ --event-bus-name mumbai-orders-bus --ids notify-team --region $AWS_REGIONaws events delete-rule --name new-order-processor \ --event-bus-name mumbai-orders-bus --region $AWS_REGION aws events delete-archive --archive-name mumbai-orders-archive --region $AWS_REGIONaws events delete-event-bus --name mumbai-orders-bus --region $AWS_REGIONaws sns delete-topic --topic-arn $TOPIC_ARN --region $AWS_REGIONProduction Best Practices & Common Pitfalls
Common MistakeTreating a schedule as a guarantee that earlier work succeeded. A schedule fires whether or not the last run failed, and delivery is at least once. Build idempotent targets that can safely run twice.
SecurityScope every target permission tightly. Grant EventBridge only the
sns:Publishorlambda:InvokeFunctionit needs on the exact resource, and restrict who can callPutEventson shared buses.
- Give failing targets a dead-letter queue. Attach an SQS queue to each target so events that cannot be delivered are kept instead of lost.
- Test patterns before deploying. Run
test-event-patternwith a realistic event, because a pattern with one wrong field silently matches nothing. - Keep one bus per domain. A Custom bus for orders and another for payments keeps rules readable and permissions narrow.
- Use Pipes for plain routing. If a Lambda only reads a stream, filters and forwards, a Pipe does the same with configuration instead of code to maintain.
- Design events as contracts. Include stable fields such as
orderIdand a version, and treat changes like API changes, since many consumers depend on them.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
aws events put-rule |
Create or update a rule |
aws events put-targets |
Attach targets to a rule |
aws events put-events |
Publish custom events |
aws events test-event-pattern |
Check whether an event matches a pattern |
aws events list-rules --event-bus-name NAME |
List rules on a bus |
aws events create-archive |
Archive a bus for replay |
aws events describe-rule --name NAME |
Inspect a rule's state and pattern |
| Problem | Likely cause | Fix |
|---|---|---|
| Rule never fires | Pattern mismatch or wrong bus | Run test-event-pattern and confirm the bus |
| Target gets nothing | Missing permission on the target | Add a resource policy for events.amazonaws.com |
put-events shows failures |
Malformed Detail or missing bus name | Check FailedEntryCount and the error message |
| S3 events never arrive | EventBridge notifications are off | Turn on EventBridge notifications for the bucket |
| Cannot replay events | No archive existed at the time | Create the archive before you need it |