Amazon SNS Message
Filtering lets twtech to control
which subscribed endpoints receive messages based on message attributes —
so that not every subscriber receives
every message.
This is especially useful in fan-out patterns with SQS,
Lambda, or HTTP subscribers.
How Message Filtering Works
- Publishers
send messages to an SNS topic with message attributes.
- Subscribers
define filter policies that determine which messages they want.
- SNS evaluates these filters and delivers only the matching messages.
Example Scenario
twtech is publishing order events:
- Type: order_created, order_cancelled, order_shipped
twtech has 3 SQS queues:
- One only needs order_created
- One only wants order_shipped
- One wants everything
1.
Message With Attributes
# json
{
"Message": "Order #12345 was
created",
"MessageAttributes": {
"eventType": {
"DataType": "String",
"StringValue": "order_created"
}
}
}
2.
Filter Policy Example
- For "order_created" only:
# json
{
"eventType": ["order_created"]
}
- For "order_created" or
"order_shipped":
# json
{
"eventType": ["order_created",
"order_shipped"]
}
- To receive all messages, use no filter or
an empty policy {}.
Setting Filter Policy (via AWS CLI)
# bash
aws sns
set-subscription-attributes \
--subscription-arn <sub-arn> \
--attribute-name FilterPolicy \
--attribute-value '{"eventType":
["order_created"]}'
Supported
Filter Types
- String
match
- String prefix
match ("anything":
[{"prefix": "error"}])
- Numeric conditions
("amount":
[{"numeric": [">=", 100]}])
- Existence tests
("status":
[{"exists": true}])
- Anything-but
filters ("eventType":
[{"anything-but": "order_cancelled"}])
twtech
Notes and Tips
- Filter policies are evaluated server-side, no
cost for dropped messages.
- twtech can filter on multiple attributes.
- Filters are case-sensitive.
- twtech can combine SNS filters with SQS message filtering or Lambda routing logic.
No comments:
Post a Comment