SNS filtering anything-but doesn't accept blank message-attribute
Asked Answered
S

2

8

I have a filter for a SNS subscription (SQS) where I'm using this filtering:

{"source":[{"anything-but":"SOME_STRING"}]}

This works OK, except that sometimes I have messages going to the SNS that doesn't have any message-attribute called source, and before using the filter, this subscription without filter was taking it.

So, in conclusion, I would like to find a way where my filter takes the message if it doesn't have this "source" attribute or in case it have it, it should be anything-but SOME_STRING.

Thank you!

Splice answered 24/10, 2018 at 2:23 Comment(0)
V
4

Your requirement is to filter and pass messages if:

  1. The source attribute is missing
  2. The source attribute exists and it should be anything-but SOME_STRING.

Amazon SNS Subscription policy accepts messages under the following conditions.

1) Each attribute name in a filter policy matches an attribute name assigned to the message.

2) For each matching attribute name, at least one match exists between the following:

--> the values of the attribute name in the filter policy

--> the message attributes

As you can see in the given documentation for SNS Subscription filter policies, any message that does not contain the filtering attribute, or if that attribute value does not meet the policy, the message will be rejected.

According to your example, you CANNOT filter and pass messages which does not contain the "source" attribute. But you can filter if the source attribute exists and it should be anything-but SOME_STRING.

Workaround for this.

Assuming that you have access to the message generating source, make sure that every message contains the attribute "source", before pushing it to SNS. Then you can easily filter out if it is anything-but SOME_STRING.

Volnay answered 6/5, 2019 at 8:30 Comment(0)
T
3

Note Years passed already. Answering for future reference.

The following filter policy should work in the given scenario.

{
  "source": [
    { "exists": false },
    { "anything-but": "SOURCE_TO_IGNORE" }
  ]
}

EDIT: The above will work if the message contains any additional message attribute. If source is the only message attribute - it won't work.

Touter answered 13/7, 2022 at 6:49 Comment(1)
I edited your answer to add simplified detail. That's what Keet's answer explains.Harborage

© 2022 - 2024 — McMap. All rights reserved.