Skip to content

EventBridge event pattern "AND" behavior on array elements

0

Use case: My event detail contains a list of tags and I would like my rules to match on multiple tag values in an AND manner.

Sample event payload:

{
  "version": "0",
  "id": "606cde05-6fc4-4d84-6f39-6339bf5aa013",
  "detail-type": "com.example.my-event",
  "source": "/events/my-event",
  "account": "000000000000",
  "time": "2024-08-08T15:31:35Z",
  "region": "eu-central-1",
  "resources": [],
  "detail": {
    "tags": [ "A", "B", "C" ]
  }
}

I'm looking for a way to create an Event rule that matches events that contain all tag values I specify in the rule. For example:

Rule1: Tags A and B

Expected result: Matches the above event

{
  "detail": {
    "tags": [ "A", "B" ]
  }
}

Rule 2: Tags C and D

Expected result: DOES NOT match the above event (since the event does not contain tag "D")

{
  "detail": {
    "tags": [ "C", "D" ]
  }
}

However, this statement ("tags": [ "C", "D" ]) works in an OR manner. So Rule 2 matches the above event, because it contains "C", while in my use case the expectation is for it to not match, because it's missing "D".

Is there a certain combination of EventBridge event pattern operators that can be used to create the desired AND behavior, such that the event will be matched only when the event payload contains ALL elements specified in the rule pattern?

asked a year ago473 views

1 Answer
0

That’s an excellent and very relevant question, and you’ve articulated the use case clearly. The behavior you’re seeing is correct, and it stems from how Amazon EventBridge evaluates event patterns at match time. EventBridge pattern matching is structurally declarative rather than procedural, which means it evaluates arrays using OR semantics across elements by design.

Let’s break this down and then look at practical options to achieve the “AND” logic you’re aiming for.

  1. Understanding EventBridge Array Matching Behavior

When EventBridge evaluates an array in the event pattern (for example, detail.tags), it checks whether any of the values in your event payload match any of the values in your pattern array. This is why "tags": ["C", "D"] matches when only "C" is present.

Here’s how the matching logic works conceptually:

pattern.value is an array → logical OR across its members.

event.value is an array → the pattern matches if any element in event.value satisfies any condition in pattern.value.

This design is intentional to keep event filtering lightweight and declarative for large-scale distributed systems.

You can see this behavior described in the AWS documentation here: AWS EventBridge event pattern matching logic : https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html

  1. Why native “AND” semantics aren’t directly supported

EventBridge rules are evaluated using a declarative JSON-based matching engine that doesn’t support nested boolean operators such as AND or NOT for elements within the same field. Once EventBridge detects a field match, the rule is triggered, regardless of whether other elements in that array also match.

In other words, EventBridge cannot currently enforce “contains all these values” logic for array fields within a single pattern expression.

  1. Workarounds that achieve the desired behavior

There are a few ways to model your event or adjust your matching logic to achieve the equivalent of AND semantics:

Option A: Normalize your event payload

If you control the event structure, consider adding a flattened metadata field that describes your tag combinations explicitly. For example:

"detail": { "tags": ["A", "B", "C"], "tagSet": "A_B_C" }

Your rule can then match using:

"detail": { "tagSet": [{ "prefix": "A_B" }] }

This keeps pattern evaluation simple and efficient.

Option B: Use multiple rules and a step function or Lambda filter

If you need to process events dynamically without changing the producer, you can route all events through an EventBridge rule that captures the broader “superset” of tags (e.g., any of A, B, C, D), and then apply the AND logic inside an AWS Lambda function or Step Functions state machine.

The Lambda handler can then validate:

required_tags = {"A", "B"} event_tags = set(event["detail"]["tags"]) if required_tags.issubset(event_tags): # Proceed with workflow

This hybrid approach balances flexibility with maintainability and scales well for large event volumes.

Option C: Use an attribute flattening or enrichment layer

If you’re using services like Amazon EventBridge Pipes or AWS Glue as part of your event ingestion pipeline, you can enrich or preprocess events before routing. That enrichment can inject deterministic flags (for example, "hasTags": {"A": true, "B": true, "C": true}), which you can then match individually in your pattern using:

"detail": { "hasTags": { "A": [true], "B": [true] } }

This gives you logical AND semantics across fields.

  1. Future-facing considerations

EventBridge continues to evolve with new event pattern operators (such as prefix, anything-but, and exists). As of now, “array contains all” semantics aren’t part of the platform’s native feature set, but AWS has been incrementally expanding event pattern expressiveness. I’d recommend keeping an eye on the EventBridge release notes and documentation here: EventBridge new features and updates : https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-whats-new.html

  1. Summary

To recap:

EventBridge evaluates arrays using OR logic, not AND.

There is no built-in way to enforce “must contain all” within a single field pattern.

You can implement this behavior either by modifying the event schema, using downstream filtering logic in Lambda or Step Functions, or preprocessing events with EventBridge Pipes or Glue.

By thinking about this as a pattern design problem rather than a rule syntax limitation, you’ll end up with a solution that’s more adaptable and easier to maintain as your event architecture grows.

You’re approaching this exactly the right way; thinking deeply about how event semantics affect downstream workflows. That mindset is what leads to robust, scalable event-driven designs.

I can also outline a pattern library that compares the pros and cons of declarative versus procedural filtering in EventBridge for different use cases (for example, RAG pipelines, IoT telemetry, or FinOps event ingestion). It’s a great mental model for designing future-proof event systems.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.