amazon-sns

How to create a SNS Filter Policy that matches nested message attributes?


I've created an SNS topic, and I'd like to subscribe to it with a filter policy that matches a nested attribute. For example, given a message like this:

{
  "foo": {
    "bar": "baz"
  },
  "quux": "vorp"
}

I'd like to match only messages where the bar attribute of foo is equal to baz.

The documentation I've found so far only mentions matching attributes specified at the top level. I'm interested in a nested attribute. For the purposes of this question, let's assume I don't control the structure of the message.


Solution

  • It's possible to filter based on the payload.

    That's explained here: https://aws.amazon.com/tutorials/filter-messages-published-to-topics/

    An example of filtering:

    Let's say your original message is like:

    {
      "eventType": "SOMETHING",
      "anyOtherAttribute": "bla..."
    }
    

    Then the filter for an exact match will be:

    {
      "eventType": [    
          "SOMETHING"    
      ]
    }
    

    It's also possible to have other types of filtering, like by prefix:

    {
      "eventType": [
        {
          "prefix": "SOMETH"
        }
      ]
    }
    

    Note that the structure of the JSON in the filtering is a litte different from the original payload. That's what is expected by AWS.

    Check out [Message Filtering Operator] at https://aws.amazon.com/blogs/compute/message-filtering-operators-for-numeric-matching-prefix-matching-and-blacklisting-in-amazon-sns/.

    Nested filtering seems to also be supported (tough I haven't tested it).

    So from the original question, the filtering should be:

    {
      "foo.bar": ["baz"]
    }
    

    Configuration

    In AWS WebConsole, it's found in SNS -> Subscriptions -> [select one] -> Subscription filter policy.