By default, an Amazon SNS topic subscriber receives every message that's published to the topic. To receive only a subset of the messages, a subscriber must assign a filter policy to the topic subscription.
A filter policy is a JSON object containing properties that define which messages the subscriber receives. Amazon SNS supports policies that act on the message attributes or on the message body. Earlier filter policy option was only available on message attribute level not on the message body, Now Amazon introducing payload-based message filtering for Amazon SNS
We will use below example to implement payload-based message filtering in Amazon SNS
Step 1: Create Orders topic
- Open the AWS Management Console for SNS in new table or window.
- Enter a topic name
Orders
- Select Standard topic for the type of topic you want to create.
- Select Create topic
Step 2: Create Orders queue
- Open the AWS Management Console for SQS in new table or window.
- Enter a queue name
Orders
- Select Standard queue for the type of queue you want to create.
- Select Create queue
Step 3: Create Orders-EU queue
- Open the AWS Management Console for SQS in new table or window.
- Enter a queue name
Orders-EU
- Select Standard queue for the type of queue you want to create.
- Select Create queue
Step 4: Subscribe the Orders queue to the Orders SNS topic
- Select Orders queue and open SNS subscription tab.
- Select Subscribe to Amazon SNS topic
- Choose Orders topic from dropdown list, then choose save.
Step 5: Subscribe the Orders-EU queue to the Orders SNS topic
Repeat step 4 to subscribe the Orders-EQ queue to the Orders SNS topic.
Step 6: apply payload-based message filtering
- Open Orders topic.
- Select Orders-EU subscription and then choose Edit.
- Expand the Subscription filter policy section and enable Subscription filter policy.
- Select Message body and paste below JSON in JSON editor.
{
"location": ["EU"]
}
- Select Save changes.
We have configured SNS Topic Orders, and two SQS queue subscriptions Orders queue and Orders-EU queue. Now we will send messages to the Orders SNS topic which will redirect message to appropriate queue based on filter criteria.
Step 7: Publish test messages to Amazon SNS
- Sample message for US location
{
"location": "US",
"OrderId": "132456-564123-789456-897645",
"Amount": 105,
"OrderDate": "2022-12-28"
}
- Sample message for EU location
{
"location": "EU",
"OrderId": "789456-897645-132456-564123",
"Amount": 111,
"OrderDate": "2022-12-28"
}
Step 8: Verify message delivery
We have not applied any filter in Orders queue, hence we should received both the messages in Orders queue and only 1 message in Orders-EQ.
Step 9: Cleanup
- Delete Orders SNS topic.
- Delete both queue (Orders and Order-EQ)
Conclusion
The new payload-based message filtering option in SNS empowers subscribers to express their SNS subscription filter policies in terms of the contents of the message.
Happy cloud computing.