12

I've stuck the problem with defining a rule for matching my events. Googled, tested.

Let's say, we've the following event which contains the object user in the array events:

{
    "version": "0",
    "...": "...",
    "detail": {
        "events": [
            {
                "user": {
                    "id": "5efdee60b48e7c1836078290"
                }
            }
        ]
    }
}

Is there any way to match the user.id in an EventBus rule? I've already tried to use the following rule which is not valid:

{
  "detail": {
    "events": [
      {
        "user": {
          "id": [
            "5efdee60b48e7c1836078290"
          ]
        }
      }
    ]
  }
}

then,

{
  "detail": {
    "events[0]": {
      "user": {
        "id": [
          "5efdee60b48e7c1836078290"
        ]
      }
    }
  }
}

also no effect.

I don't want to give up, but I'm tired with it ;)

zenbeni
  • 7,019
  • 3
  • 29
  • 60
dobeerman
  • 1,354
  • 15
  • 27

3 Answers3

15

This pattern should work to match this event:

{
  "detail": {
    "events": {
      "user": {
        "id": [
          "5efdee60b48e7c1836078290"
        ]
      }
    }
  }
}
Tom M
  • 151
  • 1
  • 4
2

Today, EventBridge only supports matching simple values (string, integer, boolean, null) with an array. You can read more in the service documentation.

Den
  • 16,686
  • 4
  • 47
  • 87
1

I did some playing around with your example but I can't make it work. Based on reading the Arrays in EventBridge Event Patterns I have to conclude that matching inside arrays with complex values is not possible.

The quote that seems to confirm this is "If the value in the event is an array, then the pattern matches if the intersection of the pattern array and the event array is non-empty."

And from the Event Patterns page "Match values are always in arrays." So if your pattern is an array and the value in the event is also an array (this is the example you gave), a "set" based intersection test is performed. Your pattern would have to match the entire array entry, not just a single field like you have in the example.

tsdorsey
  • 557
  • 5
  • 14
  • 1
    To do the testing there is a test method on the EventBridge object in the SDK. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EventBridge.html#testEventPattern-property – tsdorsey Jul 03 '20 at 18:20
  • 1
    Finally, I solved this problem with the lambda that acts as a mapper of the data of my array into a new object without array, then I send this new object as an event to another eventbus. – dobeerman Jul 03 '20 at 21:02
  • @dobeerman How will the mapping via lambda help if the array size is of lets say 10 elements? Will the lambda send 10 different events in that case? – Aniket Kapse Feb 07 '22 at 12:15
  • @AniketKapse it will send only one event with multiple entries – dobeerman Feb 08 '22 at 13:03