EventBridge events aren’t just ephemeral messages; they can be a persistent, searchable log of your application’s behavior, and a powerful tool for debugging.
Let’s see this in action. Imagine you have an OrderCreated event flowing through your system. You want to capture this event and be able to replay it later to test a new version of your order processing Lambda function without impacting live orders.
Here’s the setup:
-
EventBridge Rule:
{ "Name": "CaptureOrderCreatedEvents", "Description": "Captures all OrderCreated events for archival and replay", "EventPattern": "{\n \"source\": [\"com.mycompany.orders\"],\n \"detail-type\": [\"OrderCreated\"]\n}", "State": "ENABLED", "EventBusName": "default" }This rule is listening for events from
com.mycompany.orderswith thedetail-typeofOrderCreatedon thedefaultevent bus. -
EventBridge Archive:
aws events put-archive --archive-name CaptureOrderCreatedArchive --event-source-pattern '{"source": ["com.mycompany.orders"], "detail-type": ["OrderCreated"]}' --region us-east-1This command creates an archive named
CaptureOrderCreatedArchivethat will store events matching the specified pattern. The archive is automatically associated with the rule that matches theevent-source-pattern. -
Target for Archival: When you create the rule, you need to specify the archive as a target. In the AWS console, you’d select "EventBridge archive" as the target type and choose
CaptureOrderCreatedArchive. Programmatically, this would be part of theput-ruleandput-targetsAPI calls.- Rule Target:
This target ARN points to the archive you created.{ "Id": "ArchiveTarget1", "Arn": "arn:aws:events:us-east-1:123456789012:archive/CaptureOrderCreatedArchive" }
- Rule Target:
Now, whenever an OrderCreated event is published, it will be captured by the rule and automatically sent to the CaptureOrderCreatedArchive.
The Mental Model:
Think of EventBridge as a central nervous system for your cloud applications. Events are the signals, and rules are the reflexes that react to specific signals. Archives are like a high-fidelity black box recorder for these signals.
- Event Bus: The conduit where events flow. The
defaultbus is the one you get out-of-the-box. You can create custom event buses for better isolation. - Events: JSON payloads describing something that happened. They have a
source,detail-type, anddetail(the actual data). - Rules: These are the listeners. They define patterns (using JSON or CDK/CloudFormation) to match specific events.
- Targets: What happens when a rule matches an event. This could be a Lambda function, an SQS queue, an SNS topic, or, in our case, an EventBridge Archive.
- Archive: A persistent, immutable storage for events that match a specific pattern. It’s not just a raw log; it’s structured storage that EventBridge manages.
Replaying Events:
The real power comes when you need to debug or test. Suppose you’ve deployed a new version of your order processing Lambda function and want to test it against past OrderCreated events.
-
Create a Replay: You’ll need to create a replay request, specifying the archive and the time range of events you want to replay. You’ll also define a replay name and a destination event bus (often a custom, isolated bus for testing).
aws events start-replay --replay-name "ReplayOrderCreatedTest-v2" \ --event-source-arn "arn:aws:events:us-east-1:123456789012:archive/CaptureOrderCreatedArchive" \ --destination-event-bus-arn "arn:aws:events:us-east-1:123456789012:event-bus/test-replay-bus" \ --from-event-timestamp "2023-10-27T10:00:00Z" \ --to-event-timestamp "2023-10-27T12:00:00Z" \ --region us-east-1This command starts a replay named
ReplayOrderCreatedTest-v2. It pulls events fromCaptureOrderCreatedArchivethat occurred between 10:00 AM and 12:00 PM UTC on October 27, 2023, and sends them to a custom event bus namedtest-replay-bus. -
Target the Replay: On your
test-replay-bus, you’d create a rule that matches theOrderCreatedevents and points to your new Lambda function (the one you want to test).{ "Name": "TestNewOrderProcessor", "EventBusName": "test-replay-bus", "EventPattern": "{\n \"source\": [\"com.mycompany.orders\"],\n \"detail-type\": [\"OrderCreated\"]\n}", "State": "ENABLED", "Targets": [ { "Id": "NewOrderProcessorLambda", "Arn": "arn:aws:lambda:us-east-1:123456789012:function:my-new-order-processor-function" } ] }Now, as the replay progresses, your new Lambda function will be invoked with those historical
OrderCreatedevents.
The most surprising thing about EventBridge Archives and Replays is that they don’t just replay events; they re-emit them as if they were happening live. This means any downstream services that are listening to those events via rules on the destination bus will also react as if the event is new. This behavior is critical for simulating real-world scenarios where an event triggers a chain reaction.
Once your replay is complete and you’ve analyzed the results, you’ll likely want to investigate how to manage the lifecycle of these archives to control costs.