Fanning Out Messages with Amazon SNS Topics and Subscriptions
This lesson explains how Amazon SNS works as a publish/subscribe service. Publishers send a message to a topic, and SNS pushes a copy to every subscriber, so one event can reach many consumers without the publisher knowing who they are.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the subscription types that SNS can deliver to.
- Explain how the fan-out pattern combines one SNS topic with several SQS queues.
- Explain how subscription filter policies deliver only the messages each subscriber needs.
Subscribers and delivery
Amazon Simple Notification Service (Amazon SNS) is a publish/subscribe (pub/sub) service. A publisher sends a message to a topic, and SNS pushes a copy to every subscription on that topic. The publisher does not know who the subscribers are, so new consumers can be added without changing it. Subscribers can be Amazon SQS queues, AWS Lambda functions, HTTP or HTTPS endpoints, email addresses, SMS numbers, mobile push endpoints and Amazon Data Firehose streams.
Because SNS pushes messages, subscribers must be available to receive them. SNS retries failed deliveries according to a delivery policy, which differs by protocol: deliveries to SQS and Lambda are retried many times over a period of up to 23 days, while HTTP endpoints follow a configurable retry policy. A subscription can have its own dead-letter queue, an SQS queue that receives messages SNS could not deliver after all retries, so they can be inspected and redelivered rather than lost.
By default, SNS wraps each message in a JSON envelope containing metadata such as the topic ARN, message ID and timestamp, with the original message in a Message field. Consumers that expect the original body must unwrap it. Raw message delivery, a subscription setting for SQS, HTTP and Firehose subscribers, delivers the original body without the envelope.
Messages are limited to 256 KB by default. Since September 2026, a topic's MaximumMessageSize attribute can raise this to 1 MiB; a topic above 256 KiB can deliver only to SQS, Lambda and Data Firehose subscriptions, with at most 100 subscriptions. Larger payloads are stored in Amazon S3 through the SNS Extended Client Library, and the message carries a reference to the object.
Fan-out
In the fan-out pattern, a publisher sends one message to an SNS topic that has several SQS queues subscribed, one for each consuming service. For example, an order service publishes OrderPlaced, and the billing, shipping and analytics services each receive a copy in their own queue. Subscribing the services' queues, rather than the services themselves, gives each consumer important properties: it processes messages at its own pace, messages wait in its queue if it is unavailable, it retries failures independently with its own dead-letter queue, and it can be added or removed without affecting the others.
Standard topics deliver at least once and do not guarantee order. When ordering and deduplication are needed, a FIFO topic delivers messages in order within each message group, and it can deliver to FIFO or standard SQS queues.
Fan-out needs permissions on the receiving side. Each queue needs a queue policy that allows the sns.amazonaws.com service principal to call sqs:SendMessage, scoped with the condition aws:SourceArn set to the topic ARN, so that only that topic can send. If the topic or queues are encrypted with customer managed AWS KMS keys, the key policies must also allow SNS to use them; a subscription that is confirmed but never receives messages usually lacks one of these permissions.
Filter policies
A subscription filter policy delivers only the messages whose attributes (or, with FilterPolicyScope set to MessageBody, whose body) match. One topic can then serve many consumers interested in different events.
Example. The fraud-check queue receives only placed orders worth 100 or more. The publisher sets matching message attributes.
{
"eventType": ["order_placed"],
"amount": [{"numeric": [">=", 100]}]
}sns.publish(
TopicArn=topic_arn,
Message=json.dumps(order),
MessageAttributes={
"eventType": {"DataType": "String", "StringValue": "order_placed"},
"amount": {"DataType": "Number", "StringValue": "249.99"},
},
)One event, three consumers, no code changes
When an order is placed, the warehouse, the email service and the analytics team all need to know. Initially, the order service called each one directly; adding the analytics team required a code change and a new deployment.
| Subscriber | Endpoint | Filter policy |
|---|---|---|
| Warehouse | SQS queue | None (all orders) |
| Email service | SQS queue | None |
| Fraud check | SQS queue | amount ≥ 1,000 |
| Analytics | Data Firehose to S3 | None |
Now the order service publishes once to an SNS topic. New consumers subscribe without the publisher changing, and each has its own SQS queue, so a slow consumer does not affect the others.
A shipping service should receive only order messages where country is DE. How is this done without changing the publisher?
Show answer
Add a filter policy to the shipping service's subscription that matches country = DE. SNS then delivers only matching messages to that subscriber.
Exam Tips
Remember for the exam
- One event processed by several independent services points to SNS-to-SQS fan-out.
- When a subscriber needs only certain events, use a filter policy rather than separate topics.
- If an SQS subscriber receives nothing, check that the queue policy allows the topic to send.
- To deliver the original message body without SNS metadata, enable raw message delivery.