SQS Message Filtering and SNS + SQS Integration
Apply SNS subscription filter policies so each SQS consumer receives only the messages it cares about, reducing unnecessary processing.
The Problem Without Filtering
In a fan-out architecture without filtering, every SQS subscriber receives every SNS message. If your topic publishes order events for 10 different product categories but a subscriber only processes electronics orders, it still receives and must discard food and clothing messages. This wastes compute, increases costs, and adds unnecessary load to consumers. SNS subscription filter policies solve this by having SNS itself route messages to only the appropriate subscribers.
How SNS Filter Policies Work
A filter policy is a JSON object applied to an SQS or Lambda subscription. SNS evaluates the policy against each message's message attributes before delivering. If the message attributes match the filter policy, the message is delivered; if not, SNS skips that subscriber silently. Filter policies support string matching, numeric ranges, prefix matching, and the exists operator to check for attribute presence or absence.
# Filter policy: only deliver ELECTRONICS orders from US or EU
{
'category': ['ELECTRONICS'],
'region': ['US', 'EU'],
'amount': [{'numeric': ['>=', 100]}]
}All lessons in this course
- SQS Standard vs FIFO Queues
- Visibility Timeout, DLQ, and Long Polling
- SNS Topics and Fan-Out Architecture
- SQS Message Filtering and SNS + SQS Integration