Send Fanout Event Notifications
with HAQM Simple Queue Service (SQS) and HAQM Simple Notification Service (SNS)
Introduction
Implementation
1. Launch the AWS Management Console
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.

2. Start the HAQM SNS Console
If the SNS console landing page appears, click Next step.

Create an HAQM SNS Topic
In this step, you will create an HAQM 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 HAQM SNS topic whenever a new order is placed on the online store.
1. Create an SNS topic
In the Create topic page, type New-Orders, in the topic name box, then click Create topic.

2. Verify topic creation
The Topic details page confirms the topic is successfully created.

Create the HAQM SQS Queues
Now that you have created the topic with HAQM SNS, you will create HAQM 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 HAQM 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.
1. Open the HAQM SQS console
Click here to open the HAQM SQS console in a new browser window. If the SQS landing page appears, click Get Started Now. Otherwise, proceed to the next step.

2. Create Orders-for-Inventory queue
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.

3. Create Orders-for-Analytics queue
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.

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

4. Verify queue creation
The new queue now appears in the queue list.

Subscribe the Queues to the Topic
Now that you have created your two HAQM SQS queues, you need to subscribe them to the HAQM SNS topic that broadcasts notifications of new orders.
1. Subscribe the queues
From the list of queues, select the Orders-for-Inventory and Orders-for-Analytics queues. From Queue Actions, select Subscribe Queues to SNS Topic.

2. Select topic
The Subscribe to a Topic dialog box is displayed. From the Choose a Topic drop-down list, select your New-Orders HAQM SNS topic.
Your SNS topic appears in the list because you created it from the same account that you used to create your HAQM 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 HAQM SNS documentation.
Leave the Topic Region unchanged, and click Subscribe.

3. Confirm subscription
The Topic Subscription Result dialog box is displayed. Click OK.

Publish a Message to the Topic
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.
1. Open the publish page
In the HAQM SNS console New Orders topic details page, click Publish message.

2. Send the order message
Publish Message to topic page appears. In the Subject box, type Order 123-4567890-1234567. 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.

Verify the Subscription
Once a new message is published, HAQM SNS will deliver that message to every endpoint that is subscribed to the topic. In a fanout scenario like this one, the HAQM 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.
1. Open queue messages
In the HAQM 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.

2. Start message polling
Click Start Polling for Messages.

3. View message panel
The View/Delete Messages in Orders-for-Inventory dialog box appears.

4. Check message content
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.

5. Delete message
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.
6. Verify Orders-for-Analytics queue received notification
Repeat steps 1 through 5 to confirm that the Orders-for-Analytics queue also received the notification of the new order.

Delete your Resources
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.
1. Delete the topics
Open the HAQM SNS console and click Topics in the left navigation pane.
Select the New-Orders topic.
Click Delete to delete topics.

2. Confirm deletion
The Delete confirmation dialog box appears. Type delete me in the dialog box and 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).

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

4. Confirm deletion
The Delete Queues dialog box is displayed. Click Yes, Delete 2 Queues. The queues are deleted.
You can now sign out of the HAQM SQS console.

Congratulations!
You have implemented a fanout scenario using HAQM SNS and HAQM SQS. You are now ready to use HAQM SNS and HAQM SQS together to deliver messages to applications that require immediate notification of an event, or to buffer messages in HAQM SQS queues for other applications to process later.
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages