amazon-web-servicesamazon-sns

Is it possible to create an AWS SNS subscription filter to inspect both the `MessageAttributes` AND the `MessageBody`


I want to create a subscription filter policy in AWS SNS which can filter on both the MessageBody and MessageAttributes. e.g.

{
    "MessageAttributes" : {
        "attr1": ["value"]
    },
    "MessageBody": {
        "body1": ["this", "orthis", "andthis"]
    }
}

So, the filter would check (attr1 == value) AND (body1 == this OR orthis OR andthis)

As I understand, it is only possible to get the subscription filter to inspect EITHER the MessageBody OR the MessageAttributes by setting the FilterPolicyScope. But I need to filter based on a combination of MessageAttribute AND MessageBody properties.

Is this possible, if so, how could it be done?


Solution

  • No, this isn't natively possible unfortunately but there is a workaround.

    SNS currently only allows you to set the FilterPolicyScope to either MessageAttributes or MessageBody, but not both. This means you can’t directly create a filter policy that inspects both the message attributes and the message body in a single policy.

    If this is really needed, create a Lambda function that manually checks the message attributes & message body and have 2 SNS topics - one that the Lambda function is connected to for receiving and filtering messages, and another for publishing the filtered messages to your final consumers.