In this tutorial, you will learn how to set up asynchronous messaging with Amazon Simple Queue Service (Amazon SQS). Amazon SQS is the AWS service that allows application components to communicate in the cloud. You will use the Amazon SQS console to create and configure a message queue, send a message, receive and delete that message, and then delete the queue.
The AWS services you use in this tutorial are within the AWS Free Tier.
Enter the Amazon SQS Console
a. 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 queue in the search bar and select Simple Queue Service to open the console.
b. If the SQS console landing page appears, as shown on by the screenshot, click Get Started Now. If you don't see this page, skip to the next step.
Create an Amazon SQS Queue
In this step, you will create and configure an Amazon SQS queue. A queue is a reliable, highly-scalable buffer that stores messages as they travel between distributed applications or microservices. Queues help to decouple applications, connect microservices, batch tasks, or store notifications.
Our use case for this tutorial will simulate the storage of incoming orders from an e-commerce application.
a. First, we will create a simple queue that stores orders that are placed on the store. Enter Orders in the Queue Name field.
b. For this tutorial, we do not require strict ordering, so we won’t make any changes to the queue type. Leave Standard Queue selected.
c. You can configure your queue to modify settings such as retention period, maximum message size and delivery delays. For this tutorial, we will keep the default parameters. Choose Quick-Create Queue.
d. Your new queue is created and selected in the queue list.
Send Messages to the Queue
Once you have created your queue, it is ready to receive messages from the online store that capture the details of each new order.
a. Your queue is already selected in the list. From Queue Actions, select Send a Message. The Send a Message to Orders dialog box is displayed.
b. The Send a Message to Orders dialog box is displayed. On the Message Body tab, enter the following text to represent a sample order:
1 x Widget @ $29.99 USD
2 x Widget Cables @ $4.99
c. Select the Message Attributes tab to add some optional metadata about this message for easy processing. Let’s add an order type to the order. Enter Order-Type in the Name field, String in the Type field, and Online in the Value field. Click Add Attribute.
d. To send the message immediately, click Send Message. Confirmation that your message was sent is displayed in the Send a Message to Orders dialog box. Click Close.
Retrieve and Delete a Message
After you send a message to a queue, another application can consume it from the queue and do something with it. In this example, you will simply retrieve the message to view the order, and then delete it.
a. Ensure that your Orders queue is selected in the queue list. Next, from Queue Actions, select View/Delete Messages.
b. The View/Delete Messages in Orders dialog box is displayed. When you request a message from a queue, you don't specify request a specific message. Instead, you specify the maximum number of messages (up to 10) that you want to retrieve.
Click Start Polling for messages to retrieve messages from the queue.
c. Once a consumer has received and processed a message, it can be deleted from the queue. Select the message that you want to delete and then choose Delete 1 Message.
d. The Delete Messages dialog box is displayed. Check the box next to the message and click Yes, Delete Checked Messages. The selected message is deleted. Choose Close.
Delete the Queue
When you no longer need to use an Amazon SQS queue, we recommend that you delete the queue as a best practice.
a. In the queue list, select the Orders queue. Then, from Queue Actions, select Delete Queue.
b. The Delete Queues dialog box is displayed. You can still delete your queue, even though you still have messages in it. Choose Yes, Delete Queue. The queue is deleted.