Lucas Wilson-Richter
Lucas Wilson-Richter

Reputation: 2324

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.

Upvotes: 21

Views: 30665

Answers (4)

D Malan
D Malan

Reputation: 11434

You can create a subscription filter with the "Message body" filter type.

For your example, to match only messages where the bar attribute of foo is equal to baz in this message:

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

Your filter would look like this:

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

Upvotes: 4

Otavio Ferreira
Otavio Ferreira

Reputation: 966

Now, SNS can filter events based on their payload as well. With this release, the SNS filter policy can handle property nesting too. For more information, please take a look at: https://aws.amazon.com/blogs/compute/introducing-payload-based-message-filtering-for-amazon-sns/

Upvotes: 1

RicardoS
RicardoS

Reputation: 2118

As of 2022, it's possible to filter based on the payload.

That's mentioned here: https://docs.aws.amazon.com/sns/latest/dg/sns-message-filtering.html

An Example of filtering:

Let's say your original message is like:

{
  "eventType": "SOME_STRING",
  "otherAttribs": "bla..."
}

Then the filter can be something like:

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

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

Upvotes: 5

Michael - sqlbot
Michael - sqlbot

Reputation: 179244

Subscription filters don't act on the message (body, payload). They only act on the message attributes.

Message attributes are not complex objects... their only types are string, string array, number, and binary. If a message attribute contains a serialized object (e.g. JSON), subscription filters aren't designed to support extracting/matching the serialized data inside.

Upvotes: 25

Related Questions