Stop 11 · Serverless & Event-Driven AppsTopics · 6/10

Pub/Sub With Amazon SNS

Part of the AWS Roadmap

In short

When an order is placed, the kitchen, the billing team and the delivery app all need to know. Instead of calling each one, you announce it once and SNS tells everyone who subscribed.

Amazon SNS is a publish-subscribe service where a message sent to a topic is pushed to all subscribers, such as SQS queues, Lambda functions, HTTPS endpoints, email or SMS. Combining SNS with several SQS queues is the common fan-out pattern.

The jaadu on a society building balcony shouts into a megaphone while residents lean out of many windows to listen at the same moment.

Think of it like this

The society WhatsApp group

The RWA secretary in a Noida society doesn't phone 200 flats about a water cut. She posts once in the group and every member gets it. New residents just join the group, and she doesn't need to know who they are. An SNS topic is the group, publishers are the secretary, and subscribers are the members.

Watch it happen

Fan-out with SNS and SQS

Order service
SNS topic
Kitchen queue
Billing queue

Publish OrderPlaced

1/4The order service publishes once and moves on.

The big ideas

What you need to know

Push, not pull

SNS delivers to subscribers immediately. It doesn't store messages for later, so pair it with SQS when consumers might be offline.

Fan-out

One topic, many SQS queues. Each service gets its own durable copy and can scale or fail independently.

Filter policies

A subscriber can accept only messages whose attributes match, such as city = Delhi, so not everyone processes everything.

Standard vs FIFO topics

Standard topics are best-effort ordering. FIFO topics keep order within a message group and deliver to SQS queues.

See it in code

Walk through the code

bash
1TOPIC_ARN=$(aws sns create-topic --name order-events \2  --query TopicArn --output text)3 4aws sns subscribe \5  --topic-arn "$TOPIC_ARN" \6  --protocol sqs \7  --notification-endpoint arn:aws:sqs:ap-south-1:111122223333:kitchen-queue \8  --attributes '{"FilterPolicy":"{\"city\":[\"Delhi\",\"Noida\"]}","RawMessageDelivery":"true"}'9 10aws sns publish \11  --topic-arn "$TOPIC_ARN" \12  --message '{"orderId":"A1042","total":549}' \13  --message-attributes '{"city":{"DataType":"String","StringValue":"Noida"}}'

1/4Create the topic and keep its ARN in a shell variable.

AWS CLI v2. The SQS queue also needs an access policy allowing sns.amazonaws.com to send messages from this topic.

Save yourself the pain

Common mistakes

Oops

Subscribing a Lambda directly and assuming no message can be lost.

Do this instead

Lambda subscriptions retry, but for critical events put an SQS queue between SNS and the consumer, with a DLQ.

Oops

Forgetting the SQS access policy, so messages silently never arrive.

Do this instead

Allow sqs:SendMessage for the sns.amazonaws.com principal with aws:SourceArn set to your topic.

Your mission

Build a fan-out

~30 min

Quick check

Why put SQS queues behind an SNS topic instead of having services subscribe over HTTPS directly?

Go deeper

  1. 1Create a topic with an email subscription and publish a test message.
  2. 2Build a fan-out: one SNS topic delivering to two SQS queues.
  3. 3Add a subscription filter policy so one queue only gets certain messages.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge