In this tutorial, you will implement a fanout messaging scenario using Amazon Simple Notification Service (SNS) and Amazon Simple Queue Service (SQS). In this scenario, messages are "pushed" to multiple subscribers, which eliminates the need to periodically check or poll for updates and enables parallel asynchronous processing of the message by the subscribers.

To illustrate this, we will assume that you are developing a cloud-native application that sends an Amazon SNS message to a topic whenever an order is placed on an online store. The Amazon SQS queues that are subscribed to that topic will each receive identical notifications for the new order.

The AWS services you use in this tutorial are within the AWS Free Tier.

Sending fanout event notifications on AWS requires an account

Create a Free Account

AWS Free Tier includes 1,000,000 publishes for Amazon Simple Notification Service and 1,000,000 requests of Amazon Simple Queue Service.

View AWS Free Tier Details »

 

When you click here, the AWS Management Console will open in a new browser window, so you can keep this step-by-step guide open. When the screen loads, enter your user name and password to get started. Then type notification in the search bar and select Simple Notification Service to open the service console.

tmt_send-fanout-event-notifications_1a

(click to zoom)

tmt_send-fanout-event-notifications_1a

a. If the SNS console landing page appears, click Get Started Now.

tmt_send-fanout-event-notifications_1b

(click to zoom)

tmt_send-fanout-event-notifications_1b

In this step, you will create an Amazon SNS topic. A topic is a communication channel to send messages and subscribe to notifications. In this example, a sample ecommerce application will push a message to an Amazon SNS topic whenever a new order is placed on the online store. 


a.  Under Common Actions, click Create topic.  

tmt_send-fanout-event-notifications_2a

(click to zoom)

tmt_send-fanout-event-notifications_2a

b. The Create topic dialog box appears. In the Topic name box, type New-Orders, then click Create topic.

tmt_send-fanout-event-notifications_2b

(click to zoom)

tmt_send-fanout-event-notifications_2b

c. The Topic Details page appears.

tmt_send-fanout-event-notifications_2c

(click to zoom)

tmt_send-fanout-event-notifications_2c

Now that you have created the topic with Amazon SNS, you will create Amazon SQS queues that will subscribe to the topic.

When you subscribe multiple queues to a topic, each queue receives identical notifications every time a message is pushed to the topic. Services attached to those queues can then process the orders asynchronously and in parallel.

For example, an Amazon EC2 server instance attached to one of the queues could handle the processing or fulfillment of the order, while the other server instance could be attached to a data warehouse for analysis of all orders received.

To keep things simple, we won't actually attach EC2 instances to the queues in this tutorial.


a. Click here to open the Amazon SQS console in a new browser window. If the SQS landing page appears, click Get Started Now. Otherwise, proceed to the next step.

 

tmt_send-fanout-event-notifications_3a

(click to zoom)

tmt_send-fanout-event-notifications_3a

b. Our first queue will store orders for a fictional Inventory Service that keeps track of products, adding and deleting them as needed from inventory with each order.  

On the Create New Queue page, enter Orders-for-Inventory in the Queue Name field. Leave Standard Queue selected and click Quick-Create Queue.

tmt_send-fanout-event-notifications_3b

(click to zoom)

tmt_send-fanout-event-notifications_3b

c. Your new queue is created and selected in the queue list. Next, you’ll create a second queue to handle order analytics.

Click Create New Queue to create another queue to store orders for the Analytics Service.

tmt_send-fanout-event-notifications_3c

(click to zoom)

tmt_send-fanout-event-notifications_3c

d. Enter Orders-for-Analytics in the Queue Name field, and click Quick-Create Queue

tmt_send-fanout-event-notifications_3d

(click to zoom)

tmt_send-fanout-event-notifications_3d

e. The new queue now appears in the queue list.

tmt_send-fanout-event-notifications_3e

(click to zoom)

tmt_send-fanout-event-notifications_3e

Now that you have created your two Amazon SQS queues, you need to subscribe them to the Amazon SNS topic that broadcasts notifications of new orders.


a. From the list of queues, select the Orders-for-Inventory and Orders-for-Analytics queues. From Queue Actions, select Subscribe Queues to SNS Topic.

tmt_send-fanout-event-notifications_4a

(click to zoom)

tmt_send-fanout-event-notifications_4a

b. The Subscribe to a Topic dialog box is displayed. From the Choose a Topic drop-down list, select your New-Orders Amazon SNS topic.

Your SNS topic appears in the list because you created it from the same account that you used to create your Amazon SQS queues. If the SNS topic was made by another account, you could subscribe to it by using the Topic ARN. For more details, see the Amazon SNS documentation.

Leave the Topic Region unchanged, and click Subscribe.  

tmt_send-fanout-event-notifications_4b

(click to zoom)

tmt_send-fanout-event-notifications_4b

c. The Topic Subscription Result dialog box is displayed. Click OK.

tmt_send-fanout-event-notifications_4c

(click to zoom)

tmt_send-fanout-event-notifications_4c

Your queues are now subscribed to the topic. In this step, you will simulate a new order by having the fictional ecommerce application push a message to the topic with the order details.


a. In the Amazon SNS console, click on Topics.

 

tmt_send-fanout-event-notifications_5a

(click to zoom)

tmt_send-fanout-event-notifications_5a

b. Select the New-Orders topic, then click the Publish to topic button.


tmt_send-fanout-event-notifications_5b

(click to zoom)

tmt_send-fanout-event-notifications_5b

c. The Publish a Message page appears.

In the Subject box, type Order 123-4567890-1234567.

In Message format, select Raw.

In the Message field, enter the following text to represent a sample order:
1 x Widget @ $29.99 USD
2 x Widget Cables @ $4.99

Click Publish Message. A confirmation dialog box will appear.

tmt_send-fanout-event-notifications_5c

(click to zoom)

tmt_send-fanout-event-notifications_5c

Once a new message is published, Amazon SNS will deliver that message to every endpoint that is subscribed to the topic. In a fanout scenario like this one, the Amazon SQS queues are the endpoints.

In this step, you will confirm that the queues received the new order notification by viewing the message that the topic sent to the queues.


a. In the Amazon SQS console, check the box for the Orders-for-Inventory queue from the queue list. From the Queue Action drop-down, select View/Delete Messages.

 

tmt_send-fanout-event-notifications_6a

(click to zoom)

tmt_send-fanout-event-notifications_6a

b. Click Start Polling for Messages.


tmt_send-fanout-event-notifications_6b

(click to zoom)

tmt_send-fanout-event-notifications_6b

c. The View/Delete Messages in Orders-for-Inventory dialog box appears.

tmt_send-fanout-event-notifications_6c

(click to zoom)

tmt_send-fanout-event-notifications_6c

d. In the Body column, click More Details. The Message Details box contains a JSON document that contains the subject and message that you published to the topic.  

You have confirmed that the Orders-for-Inventory queue received the notification of the new order from the New-Orders topic.  

Click Close.

 

tmt_send-fanout-event-notifications_6d

(click to zoom)

tmt_send-fanout-event-notifications_6d

e. We'll assume that our fictional Inventory Service has finished processing this message, and that we can now safely delete the message from the queue. 

Click Delete 1 Message. To confirm, click Yes, Delete Checked Messages. Then click Close.

 

tmt_send-fanout-event-notifications_6e

(click to zoom)

tmt_send-fanout-event-notifications_6e

f. Repeat steps 6a through 6e to confirm that the Orders-for-Analytics queue also received the notification of the new order.

tmt_send-fanout-event-notifications_6f

(click to zoom)

tmt_send-fanout-event-notifications_6f

In this step, you will delete the resources you have created for this tutorial, which include the Topic Subscriptions, Topics, and Queues. It is a best practice to delete resources you are no longer using so you don’t incur charges.


a. Open the Amazon SNS console and click Topics in the left navigation pane.

Select the New-Orders topic.

Click the Actions drop-down list and select Delete topics.


tmt_send-fanout-event-notifications_7a

(click to zoom)

tmt_send-fanout-event-notifications_7a

b. The Delete confirmation dialog box appears. Click Delete. The topic, and its subscriptions, are deleted. You can now close the SNS browser window (but don't sign out, as you still need to delete the queues in the SQS console).

 

tmt_send-fanout-event-notifications_7b

(click to zoom)

tmt_send-fanout-event-notifications_7b

c. In the Amazon SQS console, select the Orders-for-Inventory and Orders-for-Analytics queues. From Queue Actions, select Delete Queues.


tmt_send-fanout-event-notifications_7c

(click to zoom)

tmt_send-fanout-event-notifications_7c

d. The Delete Queues dialog box is displayed. Click Yes, Delete 2 Queues. The queues are deleted.

You can now sign out of the Amazon SQS console.

tmt_send-fanout-event-notifications_7d

(click to zoom)

tmt_send-fanout-event-notifications_7d

You have implemented a fanout scenario using Amazon SNS and Amazon SQS. You are now ready to use Amazon SNS and Amazon SQS together to deliver messages to applications that require immediate notification of an event, or to buffer messages in Amazon SQS queues for other applications to process later.

 


Now that you have learned how to implement message fanout notifications using Amazon Simple Notification Service and Amazon Simple Queue Service, pick one of the following options to dive deeper into cloud messaging on AWS.

Learn more

Get an overview of the capabilities, components, and use cases for message topics on Amazon Web Services (AWS). Visit the Amazon SNS webpage »

Dive deeper

Explore how to enable applications, end-users, and devices to instantly send and receive notifications from the cloud. Read the documentation »

See it in action

Hear how customers like Capital One use Amazon SQS and Amazon SNS to decouple and scale applications. Watch the webinar »

Yes
No