Sub topic 6
Writing event patterns
This chapter assumes you know what an event looks like (source, detail-type, detail) and that a rule on an event bus owns the pattern. It also assumes you can use the AWS CLI and read JSON.
How a pattern matches
An event pattern is a JSON document that has the same shape as the events you want to match. It lists only the fields you care about. EventBridge ignores every other field in the event.
Two rules govern all matching:
- Fields at the same level are combined with AND. Every field you list must match.
- Values inside one array are combined with OR. The event matches if its value equals any entry.
Leaf values must be written as arrays, even when there is only one value. "source": "aws.s3" is not a valid pattern, but "source": ["aws.s3"] is.
A pattern with content filtering
This pattern matches OrderPlaced events from the orders application where the amount is at least 100 and the status is anything except test.
{
"source": ["com.example.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"amount": [{ "numeric": [">=", 100] }],
"status": [{ "anything-but": ["test"] }]
}
}Nested objects in the event become nested objects in the pattern. Here detail mirrors the event's detail object.
Common matching operators
| Operator | Example | Matches when |
|---|---|---|
| Exact value | `["new", "paid"]` | The value is `new` or `paid` |
| `prefix` | `[{"prefix": "eu-"}]` | The string starts with `eu-` |
| `suffix` | `[{"suffix": ".png"}]` | The string ends with `.png` |
| `anything-but` | `[{"anything-but": ["test"]}]` | The field exists and is not `test` |
| `numeric` | `[{"numeric": [">", 0, "<=", 10]}]` | The number is in the range |
| `exists` | `[{"exists": true}]` | The field is present (`false` means absent) |
Test the pattern before using it
Save the pattern as pattern.json. Save an event as sample-event.json. The test command needs a complete event, including the envelope fields:
{
"version": "0",
"id": "11111111-2222-3333-4444-555555555555",
"detail-type": "OrderPlaced",
"source": "com.example.orders",
"account": "123456789012",
"time": "2026-11-26T18:00:00Z",
"region": "us-east-1",
"resources": [],
"detail": { "orderId": "1001", "amount": 250, "status": "new" }
}Run the test:
aws events test-event-pattern --event-pattern file://pattern.json --event file://sample-event.jsonThe command prints "Result": true or "Result": false. Change amount to 50 and run it again to see the result flip to false.
Then attach the pattern to a rule on the custom bus:
aws events put-rule --name high-value-orders --event-bus-name orders-bus --event-pattern file://pattern.jsonCommon errors
- Type mismatch. The string
"250"does not match a numeric condition, and the number250does not match the exact string"250". Match the type your producer sends. - Case sensitivity. Exact matching is case-sensitive, so
OrderPlaceddoes not matchorderplaced. - Missing field with
anything-but. If the event has nostatusfield, the condition does not match. Addexists: falseas a second alternative if absent fields should pass. - Detail sent as a string. PutEvents accepts
Detailas a JSON string, but EventBridge parses it. Your pattern still matches on its fields. If the string is not valid JSON, the entry fails. - Rules on the wrong bus. A rule only sees events on its own bus. A correct pattern on the default bus never sees events sent to
orders-bus.
Exercise
Write a pattern that matches events whose detail.region is either "eu-west-1" or "eu-central-1", and whose detail.orderId starts with "EU-".
Show answer
{"detail": {"region": ["eu-west-1", "eu-central-1"], "orderId": [{"prefix": "EU-"}]}}. The two region values are alternatives (OR) inside one array. The two detail fields are combined with AND.