No, AWS EventBridge event patterns cannot directly compare two different attributes for equality within the pattern itself. Event patterns are designed to match events based on static values or conditions applied to individual fields, not dynamic comparisons between fields.
Why It's Not Possible:
-
Syntax Limitations:
Event patterns use a JSON structure where each condition operates on a single field (e.g., "field": [ "value" ]). There is no syntax to reference two fields in a single condition.
-
Operators Are Field-Specific:
Operators like equals, prefix, or numeric comparisons (e.g., numeric) only compare a field against a fixed value, not against another field.
Example of What Doesn't Work:
{
"detail": {
"field1": { "equals": "field2" } // INVALID: "field2" is treated as a literal string, not a reference.
}
}
Workarounds:
1. Filter at the Target (Recommended)
Use a Lambda function, Step Functions, or other compute target to compare fields:
# Lambda Example (Python)
def handler(event, context):
detail = event.get('detail', {})
if detail.get('field1') == detail.get('field2'):
# Process event
else:
# Skip
- Pros: Flexible, supports complex logic.
- Cons: Events still trigger the target (costs apply even when skipped).
2. Preprocess Events with Input Transformer
If using EventBridge with targets like Lambda, use an input transformer to create a new field combining both values, then match on that:
// Input Transformer (creates a new field)
{
"combinedField": "<field1>|<field2>"
}
Then write a pattern to match when combinedField equals a specific concatenated value (e.g., "value|value").
- Limitation: Only works if you know the expected values in advance (not dynamic).
3. Stateful Filtering with Step Functions
Use EventBridge to trigger a Step Functions workflow that:
- Checks if
field1 == field2.
- Continues processing only if they match.
- Use Case: Complex workflows needing state management.
Key Takeaways:
- EventBridge patterns evaluate fields in isolation, not relationships between fields.
- For dynamic comparisons, handle logic in the target (e.g., Lambda).
- If possible, restructure upstream events to include a precomputed field (e.g.,
"isEqual": true) if equality is a known requirement.
For advanced use cases, explore Amazon EventBridge Pipes, which offer enriched filtering/transformations before reaching the target.