Sub topic 13
Archiving and replaying events
This chapter covers how to store events from a bus and send them through the bus again. It assumes you know how rules, event patterns and targets work, and that you can use the AWS CLI. Familiarity with SQS or Lambda helps for the consumer examples.
Why archive and replay
An event bus does not keep events. After EventBridge matches an event against rules and delivers it, the bus holds no copy. If a bug in a consumer corrupted data last Tuesday, you have nothing to feed it again. If you add a new target next month, it only sees new events.
An archive fixes this. It stores copies of events from one bus. A replay later re-sends events from an archive, for a time range you choose, back through the bus. The rules on the bus match the replayed events again, so targets receive them as if they were new.
Common uses:
- Recovering after a consumer bug or outage that outlasted the retry window.
- Testing a new rule or target against real traffic shapes.
- Populating a new consumer, such as a read model, with historical events.
- Reproducing a production problem in a development account.
How archive and replay fit together
The archive is a side copy. It does not change normal delivery. The replay is a separate operation that re-injects stored events into the bus.
Creating an archive
An archive belongs to one event bus. You set three things:
- The source bus, given by its ARN.
- An optional event pattern. With no pattern, the archive stores every event on the bus. With a pattern, it stores only matching events. The pattern uses the same syntax as a rule pattern.
- A retention period in days. A value of
0keeps events indefinitely. Any other value deletes events older than that many days.
This command archives only OrderPlaced events from the custom bus used in earlier chapters, and keeps them for 30 days:
aws events create-archive \
--archive-name orders-archive \
--event-source-arn arn:aws:events:us-east-1:123456789012:event-bus/orders-bus \
--event-pattern '{"source":["com.example.orders"],"detail-type":["OrderPlaced"]}' \
--retention-days 30You can inspect and change an archive afterwards:
aws events describe-archive --archive-name orders-archive
aws events update-archive \
--archive-name orders-archive \
--retention-days 90describe-archive reports the state, the event count and the size in bytes. You can change the pattern, description and retention with update-archive. You cannot change the source bus.
Choosing a pattern
Archiving everything is simple, but storage is billed by volume and retention. Narrow the pattern when a bus carries high-volume, low-value events such as heartbeats. A pattern that is too narrow has the opposite risk. A replay can only return what was stored, so an event type you excluded is gone.
Events replayed from an archive are not archived again. This prevents an archive from feeding itself.
Starting a replay
A replay needs:
- A name.
- The archive ARN as the event source.
- A start time and an end time, both in the past.
- A destination, which is the ARN of the bus the archive came from.
The destination must be the original bus. You cannot replay into a different bus, and you cannot replay across Regions in one call.
aws events start-replay \
--replay-name orders-fix-2026-11-24 \
--event-source-arn arn:aws:events:us-east-1:123456789012:archive/orders-archive \
--event-start-time 2026-11-24T00:00:00Z \
--event-end-time 2026-11-24T06:00:00Z \
--destination '{"Arn":"arn:aws:events:us-east-1:123456789012:event-bus/orders-bus"}'Check progress, and cancel if you started the wrong range:
aws events describe-replay --replay-name orders-fix-2026-11-24
aws events cancel-replay --replay-name orders-fix-2026-11-24A replay moves through the states STARTING, RUNNING, and then COMPLETED. It can also end as CANCELLING, CANCELLED or FAILED. describe-replay shows the state and, on failure, a state reason. You cannot edit a running replay.
Limiting which rules receive replayed events
By default, a replay sends events to the bus and every enabled rule that matches gets them. That is rarely what you want. If five rules feed five different systems, replaying to repair one of them re-triggers all five.
Use FilterArns in the destination to name the rules that should receive replayed events:
aws events start-replay \
--replay-name orders-fix-targeted \
--event-source-arn arn:aws:events:us-east-1:123456789012:archive/orders-archive \
--event-start-time 2026-11-24T00:00:00Z \
--event-end-time 2026-11-24T06:00:00Z \
--destination '{"Arn":"arn:aws:events:us-east-1:123456789012:event-bus/orders-bus","FilterArns":["arn:aws:events:us-east-1:123456789012:rule/orders-bus/high-value-orders"]}'With FilterArns, only the listed rules evaluate the replayed events. Other rules on the bus ignore them. Rules must belong to the destination bus.
What a replayed event looks like
A replayed event keeps its original id, time, source, detail-type and detail. EventBridge adds one top-level field, replay-name, with the name you gave the replay. Original live events do not have this field.
This field is how you tell live events from replayed ones, in both rules and consumers.
To keep a rule from reacting to replays, match only events where the field is absent:
{
"source": ["com.example.orders"],
"detail-type": ["OrderPlaced"],
"replay-name": [{ "exists": false }]
}To build a rule that handles only replays, for example one that sends events to a test target, use "exists": true instead.
Replayed events are not guaranteed to arrive in their original order, and the replay rate is not something you configure. Do not build consumers that depend on strict ordering across a replay.
Making consumers safe to replay
Because replayed events keep the original id, a consumer can detect duplicates. The retry behavior from the previous chapter already means a consumer may see the same event more than once, so idempotency is needed anyway. A replay makes it unavoidable.
This Lambda handler skips events it has already processed. It stores the event id in a DynamoDB table with a conditional write:
import os
import boto3
from botocore.exceptions import ClientError
table = boto3.resource("dynamodb").Table(os.environ["PROCESSED_TABLE"])
def handler(event, context):
event_id = event["id"]
try:
table.put_item(
Item={"eventId": event_id},
ConditionExpression="attribute_not_exists(eventId)",
)
except ClientError as err:
if err.response["Error"]["Code"] == "ConditionalCheckFailedException":
print(f"Skipping duplicate event {event_id}")
return {"status": "duplicate"}
raise
replay = event.get("replay-name")
print(f"Processing {event_id} (replay: {replay})")
# Business logic goes here.
return {"status": "processed"}This pattern has a catch. If your goal is to reprocess events because the earlier processing was wrong, the deduplication table will reject them. In that case either clear the relevant keys first, or include replay-name in the key so each replay counts as a new attempt.
Permissions and encryption
The principal that creates archives and starts replays needs the events:CreateArchive, events:StartReplay, events:DescribeReplay and events:CancelReplay actions. For an encrypted archive, the key policy must also let EventBridge use the key. Archives can use a customer managed KMS key, which you set when you create the archive. Replays do not use your targets' permissions in a new way. Each target is invoked through the same resource policy or execution role as live delivery.
Cost and limits to plan for
- You pay for archive storage and for the events processed into the archive. A long or indefinite retention on a busy bus grows steadily.
- Replayed events are billed as custom events on the bus, and any target invoked has its own charges.
- An archive can hold events from only one bus, and the replay destination must be that bus.
- Replay start and end times must fall inside the period the archive covers.
Check current quotas and pricing in the EventBridge documentation before you rely on specific numbers.
Summary of the workflow
- Create the archive early, with a pattern that covers the events you might need.
- Set retention to match how far back you might need to recover.
- For a recovery, pick a precise time range and name the replay after the reason.
- Use
FilterArnsto restrict which rules receive events. - Make the consumer idempotent and watch
replay-namein logs. - Check
describe-replayuntil the state isCOMPLETED.
Exercise
A bus has three rules: one writes orders to a database, one emails the customer, and one updates an analytics dashboard. A bug corrupted the dashboard data between 02:00 and 04:00 UTC yesterday. You start a replay of that range with no FilterArns. What goes wrong, and how do you fix the command?
Show answer
Without FilterArns, every enabled rule that matches the replayed events receives them. The database rule writes the orders again and the email rule sends a second email to each customer. Add FilterArns to the --destination JSON with only the analytics rule's ARN, for example {"Arn":"<bus ARN>","FilterArns":["<analytics rule ARN>"]}. Also set the start time to 02:00 and the end time to 04:00 UTC on that day.
Exercise
You want a rule that sends events to a debugging queue only when they come from a replay. What event pattern do you use, and which field makes it work?
Show answer
Match the top-level replay-name field with {"replay-name": [{"exists": true}]}, combined with any source or detail-type conditions you need. EventBridge adds replay-name only to replayed events, so live events do not match.