Sub topic 6
Writing event patterns
This chapter assumes you can read JSON, have created an event bus and a rule with the AWS CLI, and know the envelope fields of an EventBridge event (source, detail-type, detail). Here you will learn how a pattern is evaluated against an event, which operators exist, and where patterns commonly go wrong.
How a pattern is evaluated
An event pattern is a JSON document that mirrors the shape of the events you want to match. A rule holds one pattern. EventBridge checks every event arriving on the rule's bus against it. Only matching events go to the rule's targets.
Three rules govern all matching:
- Fields you leave out are ignored. An event can contain any number of extra fields, and the pattern still matches.
- Different fields are combined with AND. Every field named in the pattern must match.
- The values for one field are an array, and the array is an OR. The field matches if any listed value matches.
Because values are always wrapped in an array, "source": "com.example.orders" is invalid. Write "source": ["com.example.orders"]. Nested objects such as detail are the exception: they are written as objects, and the arrays appear at the leaves.
Matching envelope fields
You can match on the top-level fields source, detail-type, account, region, resources, time and id. In practice most rules use source and detail-type, which narrow traffic cheaply before any detail logic applies.
{
"source": ["com.example.orders", "com.example.billing"],
"detail-type": [{ "prefix": "Order" }]
}This matches events from either source whose detail-type starts with Order, such as OrderPlaced or OrderCancelled. Matching is case-sensitive, so orderplaced does not match OrderPlaced.
Matching on detail
Nest the pattern under detail the same way the event nests its data. The pattern from this tutorial's running example is:
{
"source": ["com.example.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"amount": [{ "numeric": [">=", 100] }],
"status": [{ "anything-but": ["test"] }]
}
}The rule matches an OrderPlaced event from com.example.orders when detail.amount is at least 100 and detail.status is anything other than test. All four conditions must hold.
Exact values
A bare value matches exactly. Strings, numbers, booleans and null are all allowed, and types matter. [100] matches the number 100 but not the string "100". [null] matches a JSON null but not an empty string. [""] matches an empty string.
You can mix plain values and operators in one array. "status": ["new", {"prefix": "re"}] matches new, retry, refund and so on.
Content-filtering operators
| Operator | Example | Matches |
|---|---|---|
| `prefix` | `{"prefix": "us-"}` | Strings that start with `us-` |
| `suffix` | `{"suffix": ".png"}` | Strings that end with `.png` |
| `equals-ignore-case` | `{"equals-ignore-case": "Paid"}` | `paid`, `PAID`, `Paid` |
| `anything-but` | `{"anything-but": ["test", "demo"]}` | Any value not in the list. It also accepts a single value, or a `prefix` or `suffix` object. |
| `numeric` | `{"numeric": [">", 0, "<=", 500]}` | Numbers in a range. Comparison operators are `=`, `<`, `<=`, `>`, `>=`. |
| `exists` | `{"exists": true}` | Events where the field is present |
| `cidr` | `{"cidr": "10.0.0.0/24"}` | IP address strings inside the range |
| `wildcard` | `{"wildcard": "*/prod/*.json"}` | Strings that fit a `*` pattern |
Operators apply to leaf values only, never to a whole object.
Combining conditions
Different fields are ANDed and values within one field are ORed. A condition that spans different fields needs $or, which takes a list of sub-patterns:
{
"source": ["com.example.orders"],
"$or": [
{ "detail": { "amount": [{ "numeric": [">=", 1000] }] } },
{ "detail": { "customerTier": ["gold"] } }
]
}This matches orders that are either large or from gold-tier customers. Without $or, you would need two rules.
Two further points:
- A range on one field is one operator:
{"numeric": [">", 0, "<", 100]}. Putting two separate numeric objects in the array makes it an OR, which is rarely what you want. anything-butis the only built-in negation. To say "not this field combination", restructure with$oror filter in the target.
Arrays and missing fields
If an event field holds an array, the pattern matches when any element matches. With "detail": {"tags": ["urgent", "gift"]} in the event, the pattern "tags": ["gift"] matches. You cannot require that an array equals an exact list.
Missing fields need care:
- A pattern that names a field the event lacks does not match, unless you use
{"exists": false}. {"exists": false}is a way to catch events that lack a field, such as orders with nocouponCode.existsworks on leaf fields. It does not test whether an object or an empty array is present.anything-butalso fails when the field is absent."status": [{"anything-but": ["test"]}]does not match an event with nostatusat all. Add{"exists": false}to the same array if you want those events too.
Testing a pattern before attaching it
test-event-pattern evaluates one event against one pattern without creating a rule. The event you supply must contain the full envelope: id, account, source, time, region, resources, detail-type and detail.
Save the pattern above as pattern.json, and this event as sample-event.json:
{
"id": "11111111-2222-3333-4444-555555555555",
"account": "123456789012",
"source": "com.example.orders",
"time": "2026-11-26T18:00:00Z",
"region": "us-east-1",
"resources": [],
"detail-type": "OrderPlaced",
"detail": { "orderId": "1001", "amount": 250, "status": "new" }
}Then run:
aws events test-event-pattern \
--event-pattern file://pattern.json \
--event file://sample-event.json \
--query Result --output textThe command prints True for this event. Change amount to 50 and it prints False. Test at least one event that should match and one that should not. Keep these sample events in your repository as regression tests for pattern changes.
When you are happy with the pattern, attach it to a rule on the correct bus:
aws events put-rule \
--name high-value-orders \
--event-bus-name orders-bus \
--event-pattern file://pattern.jsonCommon errors
- Type mismatch. A producer sends
"amount": "250"as a string, andnumericnever matches it. Fix the producer, or match the string form, but be consistent. - Bare values instead of arrays. The pattern is rejected as invalid. Wrap every leaf value in
[ ]. - Wrong
detail-typespelling or case. Exact matching gives no error, just silence. Compare the pattern to a real event. - Pattern written against
Detailas sent.PutEventstakesDetailas a JSON string, but EventBridge parses it. Write the pattern against the parsed object, not escaped text. - Expecting a negated field to match missing data. See the
anything-butnote above. - Assuming AND across array entries.
"status": ["new", "paid"]matches either value, never both. - Testing in production only. A rule that matches nothing produces no error. Check the
MatchedEventsandInvocationsmetrics in CloudWatch when a rule seems quiet.
Exercise
Write a pattern that matches events from source com.example.orders with detail-type OrderPlaced, where detail.status is new or retry and detail.couponCode is absent. What happens to an event whose status is retry and whose couponCode is SAVE10?
Show answer
{"source": ["com.example.orders"], "detail-type": ["OrderPlaced"], "detail": {"status": ["new", "retry"], "couponCode": [{"exists": false}]}}. The event with couponCode set to SAVE10 does not match, because every field in the pattern is ANDed and the exists: false condition fails.
Exercise
A rule uses "detail": {"amount": [{"numeric": [">=", 100]}]}, but a producer publishes "amount": "250". Does the rule match, and what are two ways to fix it?
Show answer
It does not match, because the value is a string and numeric only compares JSON numbers. Fix the producer to send the number 250, or change the pattern to match the string form with operators such as prefix (which is awkward for ranges), so correcting the producer is better.
Exercise
Why would "detail": {"status": [{"anything-but": ["test"]}]} fail to match an event that has no status field, and how do you include such events?
Show answer
Operators other than exists: false need the field to be present. Add {"exists": false} to the same array: "status": [{"anything-but": ["test"]}, {"exists": false}].