darnitdiy
darnitdiy

Reputation: 559

AWS SNS Subscription Filter policy checking a key in Message Attributes does NOT exist - possible?

We have two types of SNS messages coming in:

1. has MessageAttributes empty like this:

"MessageAttributes": {}

2. has MessageAttributes coming in like this:

                "MessageAttributes": {
                    "Generator": {
                        "Type": "String",
                        "Value": "some-service"
                    }
                }

I would like to use a filter subscription policy that ignores the second type but passes the first type to the subscriber.

So I tried this for the policy:

{
  "Generator": [
    {
      "exists": false
    }
  ]
}

I thought this would mean it will only pass along messages that do NOT contain the Generator key in MessageAttributes

However I am seeing now that no messages are getting passed along.

The AWS Subscription Filter docs seem to support this as a solution, but they only show the opposite way of checking that a key does exist, so I'm not sure if they support checking a key doesn't exist: https://docs.aws.amazon.com/sns/latest/dg/sns-subscription-filter-policies.html#attribute-key-matching

Is this possible?

Upvotes: 3

Views: 7996

Answers (2)

JosephL
JosephL

Reputation: 5973

The answer from @David Adams is out of date. See the Attribute key matching docs.

Use "exists": false to return incoming messages that don't include the specified attribute.

It is now possible to exclude any messages that have a particular key by using the policy:

{ 
  "key": [
    {
      "exists": false
    }
  ]
 }

Upvotes: 3

David Adams
David Adams

Reputation: 21

Late response but may be helpful to someone.

Filtering out by lack of existance is not possible. See the bottom of https://docs.aws.amazon.com/sns/latest/dg/sns-subscription-filter-policies.html#attribute-key-matching

Note: You cannot use the exists operator to match messages in which an attribute does not exist. Filtering will NOT match any messages if you set [{"exists": false}].

You could pass a string 'null' or similar to the generator attribute if it is non existant maybe?

Upvotes: 1

Related Questions