Amazon Simple Notification Service (SNS) Fanout Pattern Tutorial

This tutorial covers how to use the Amazon Simple Notification (SNS) service to create a Fanout pattern. We will create a single SNS topic and have multiple SQS queues subscribe to it, along with an SMS and email notification.

SNS is a AWS publish-subscribe messaging service that allows a multiple subscribers to be notified about a specific event. It decouples events from the processing of those events and allows for parallel processing of events by each subscriber.

In our tutorial we will use a “New Sale” event to simulate a new order occurring at an online store and how multiple systems have to be notified about it. We will want to inform the order processing team about it, as well as the accounting team. We also want to send an email to the owner as well as an SMS.

Creating SNS Topic

Create a new SNS topic from dashboard and fill in the display name for SMS notification. This will represent a “New Sale” event that we want to inform all subscribers about.

Creating SQS Queues

Create two SQS queues that will subscribe to the SNS topic – Accounting and Order Processing, each with their own Dead Letter Queue.

Another tutorial covered how to create SQS queues.

Subscribe SQS Queues to SNS Topic

Subscribe each SQS queue to the SNS topic from the SQS dashboard:

Select the SNS topic from the drop down menu and the ARN will be auto filled:

You will get a confirmation message saying the SQS queues are subscribed to the SNS topic:

Note that if you try and subscribe to the SQS topics from the SNS dashboard, the messages won’t be received, so do the subscription from SQS.

Create SMS Subscription

Create SMS subscription to the SNS topic from the dashboard by selecting the SNS topic and adding a new SMS subscription:

There won’t be a need to confirm the SMS subscription despite a message saying to the contrary.

Create Email Subscription

Create Email subscription to the SNS topic from the dashboard by selecting the SNS topic and adding a new Email subscription. Note that you will need to verify the email subscription by clicking on a verification link in your email account before being able to receive notifications.

Publish Message

Now that all the subscriptions are set up, we can publish a message simulating a new sale that could have been triggered from another system.

After you publish the message, it will be received by all 4 subscribers.

Further Reference

Amazon Simple Notification Service

Common SNS Scenarios – Fanout

Tutorial: Subscribing an Amazon SQS Queue to an Amazon SNS Topic