Amazon SNS Message Filtering - Overview.
Scope:
- Intro,
- How Message Filtering Works,
- Architecture,
- Sample Scenario,
- Setting Filter Policy (via AWS CLI),
- Supported Filter Types,
- Tips.
Intro:
- 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.
Sample Scenario
twtech is publishing order events:
- Type: twtech_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": "twtech_order_created"
}
}
}
2.
Filter Policy Example
- For "twtech_order_created" only:
# json
{
"eventType": ["twtech_order_created"]
}
- For "twtech_order_created" or
"twtech_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 twtechFilterPolicy \
--attribute-value '{"eventType":
["twech_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": "twtech_order_cancelled"}])
twtech 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