Lesson 2 of 2 Amazon EventBridge

Testing and Observing Event-Driven Applications

This lesson explains how event-driven applications are tested by sending realistic events and checking that the right consumers react. Because processing is asynchronous, tests wait for outcomes rather than check return values.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe a workflow for testing an event-driven flow from producer to consumer.
  • Explain how archive and replay and the schema registry support testing with realistic events.
  • Identify the metrics and logs that reveal silent failures in asynchronous flows.

A testing workflow

Testing an event-driven application differs from testing a function that returns a value. The producer publishes an event and returns immediately, and the effect appears later, somewhere else, if the event matched the right rule and the consumer processed it successfully. A test therefore has to check each link in that chain: the pattern, the delivery and the consumer's result. A practical workflow consists of the following steps.

  1. Check the event pattern

    Test the pattern against a sample event with the sandbox in the EventBridge console or the TestEventPattern API. This is fast and needs no deployed resources, and it catches the most common fault: a rule that silently matches nothing.

  2. Send test events

    Send events to the deployed application with aws events put-events, aws sns publish or aws sqs send-message.

  3. Poll for the outcome

    Wait, with a timeout, for the result, such as an item appearing in a DynamoDB table. Where the consumer has no visible output, add a temporary rule that copies matching events to an SQS queue or a CloudWatch Logs log group so the test can see what was delivered.

  4. Test failure paths

    Send events designed to fail and check that they arrive in the dead-letter queue rather than disappearing.

The sample event for TestEventPattern must be a complete event envelope, including id, account, time, region, source, detail-type and resources, not just the detail object, or the test reports an error.

aws events test-event-pattern --event-pattern file://pattern.json --event file://sample-event.json
# {"Result": true}

Locally, AWS SAM can generate sample events for many AWS sources with sam local generate-event, and the consumer function can be invoked with them through sam local invoke, which tests the consumer's logic without testing the rule.

Replay and contract testing

Two EventBridge features support testing with realistic events. Archive and replay stores events that passed through a bus and re-sends those from a chosen time window. After a bug in a consumer has been fixed, replaying the events that it failed to process verifies the fix with real data and repairs the missed work. Because replayed events are delivered again, consumers must be idempotent, meaning that processing the same event twice has the same effect as processing it once.

Contract testing protects consumers from changes made by producers. The EventBridge Schema Registry stores the structure of each event type, either defined by hand or discovered automatically from events on a bus, and generates typed code bindings for languages such as Java, Python and TypeScript. Tests validate events against the schema, so a producer that renames or removes a field fails its own tests instead of breaking consumers in another team after deployment.

Observability

Failures in asynchronous flows are often silent: the producer has already received a success response, so nobody is told when an event matches no rule or a target rejects it. Event-driven applications therefore need monitoring that makes these failures visible. The following measures are recommended.

  • Include a correlation ID in every event payload and log it in each consumer, so that the path of one business transaction can be followed through several services.
  • Trace requests across services with AWS X-Ray, which shows where in the chain time is spent or errors occur.
  • Alarm on the rule metric FailedInvocations, which counts events that could not be delivered to a target, and on the number of messages in each dead-letter queue.
  • Watch Invocations and MatchedEvents for rules that should be firing; a rule whose MatchedEvents stays at zero usually has a pattern that does not match the real events.
Scenario questions

Test and observe event flows

Exam skills 3.2.3 · 4.1.2

Practise the EventBridge tools that make asynchronous failures visible and fixable.

Log in to try this activity Interactive activities are free with an account.
Worked example

A rule that never fires

A rule should trigger a Lambda function for OrderPlaced events, but the function is never invoked. The developer checks in order:

CheckFinding
Is the event on the right bus?The app publishes to orders-bus; the rule was on default
Does the pattern match a real event?aws events test-event-pattern fails: the pattern says Order Placed, the event's detail-type is OrderPlaced
Can the rule invoke the target?The function's resource policy allows events.amazonaws.com
Did invocations fail?The rule's FailedInvocations metric is zero: it simply never matched

Moving the rule to the right bus and fixing the spelling solves it. Testing patterns against real sample events in CI catches such mismatches before deployment.

Check yourself

PutEvents returns success, but the target never runs. What does the success response prove?

Show answer

Only that the bus accepted the event. It says nothing about whether a rule matched it, so the next check is the event pattern against a real sample event.

Exam Tips

Remember for the exam

  • To check that an event matches a rule, use the event pattern sandbox or TestEventPattern.
  • To reprocess past events after a fix, use EventBridge archive and replay.
  • To catch silent delivery failures, alarm on FailedInvocations and on messages in the DLQ.
  • To keep event structures consistent between teams, use the Schema Registry and its code bindings.