How to fan out events with Amazon SNS
Publish events to an Amazon SNS topic and fan them out to multiple SQS queues and Lambda subscribers, with per-subscription filter policies.
Amazon SNS is a managed publish/subscribe service. A publisher sends a message to a topic and SNS delivers a copy to every subscriber, queues, Lambda functions, HTTP endpoints, or email. The classic pattern is SNS-to-SQS fan-out: one event drives several independent processors, each with its own buffered queue.
Prerequisites
- An AWS account and the AWS CLI.
- One or more SQS queues to receive events.
Steps
1. Create a topic
aws sns create-topic --name order-events
2. Subscribe queues
aws sns subscribe --topic-arn <topic-arn> --protocol sqs --notification-endpoint <queue-arn>
Enable raw message delivery so consumers receive the bare payload, not the SNS envelope.
3. Subscribe a Lambda
aws sns subscribe --topic-arn <topic-arn> --protocol lambda --notification-endpoint <function-arn>
Grant SNS permission to invoke the function with aws lambda add-permission.
4. Publish an event
aws sns publish --topic-arn <topic-arn> --message '{"orderId":1,"type":"created"}'
Every subscriber receives a copy.
5. Add a filter policy
Subscriptions can filter on message attributes so each subscriber sees only relevant events:
{ "type": ["created"] }
Attach this as the subscription's FilterPolicy to skip irrelevant messages without delivering them.
6. Set the queue policy
Each subscribed queue needs a policy allowing the SNS topic to send messages to it. Without it, deliveries silently fail.
Verification
Publish one event and confirm a message lands in every subscribed queue and the Lambda fires. Add a filter policy, publish a non-matching event, and confirm only matching subscribers receive it. Check the topic's CloudWatch NumberOfNotificationsDelivered metric.
Next Steps
Use a dead-letter queue on subscriptions for failed deliveries, encrypt the topic with KMS, and evaluate EventBridge when you need richer routing rules.
Prerequisites
- AWS account
- Familiarity with SQS
- AWS CLI configured
Steps
- 1Create a topic
- 2Subscribe queues
- 3Subscribe a Lambda
- 4Publish an event
- 5Add a filter policy
- 6Set the queue policy