Open In App

AWS CLI SQS Commands: Key Operations and Examples

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

AWS Simple Queue Service (SQS) is a fully managed message queuing service that helps in decoupling and scaling microservices, distributed systems, and serverless applications, it facilitates the building of reliable communication between different parts of a system, which in turn assists in constructing fault-tolerant, distributed systems capable of handling variable loads. Be it developing a new application or searching for means to migrate an already existing system into the cloud, SQS gives you a scalable and secure message-queue platform.

Using SQS, one can send, store, and receive messages between software components without losing messages or requiring other services to be available. This boosts flexibility and the robustness of applications, as different parts of the system can, in one way or another, operate by themselves without affecting the other parts.

With AWS CLI, you can manage SQS queues, send and receive messages, and even set advanced features like dead-letter queues directly through your terminal. In this article, we give the most important AWS CLI commands concerning working with SQS, for which we have clear examples and step-by-step instructions so that you can get started

Primary Terminologies

  • Queue: A queue is a logical container that hosts messages temporarily while waiting for processing by a consumer. Queues help decouple different parts of an application and enable them to communicate with one another.
  • Message: The data unit sent between the producers and consumers using an SQS queue, a message can be text, JSON, binary data, or any such data up to 256 KB in size.
  • Standard Queue: A type of SQS queue that delivers high throughput, with best-effort message ordering and at least-once delivery, standard queues are recommended for use in most messaging scenarios where the exact order of message processing is not crucial.
  • FIFO Queue: First-In-First-Out. An SQS queue guarantees messages are delivered and processed only once and in the exact order, they are sent. FIFO queues are ideal for situations where strict order is essential, such as financial transactions.
  • Visibility Timeout: After a consumer receives and processes a message, the message stays in the queue but is hidden from other consumers for a short period, allowing the original receiver to delete the message. If not deleted, the message becomes visible again to other consumers after the timeout passes.
  • Dead-Letter Queue (DLQ): A queue that stores messages that are not processed correctly for whatever reason, a message is sent to the DLQ after it has been received and processed a specified number of times without being successfully handled. This aids in isolating problematic messages for further inspection.
  • Message Retention Period: The time SQS keeps a message in a queue if it is not successfully processed and deleted. Retention can be set from 1 minute to 14 days, with the default being 4 days.

Step-by-Step Process for AWS CLI Commands for SQS

Step 1: Setting Up AWS CLI

  • Install AWS CLI on your system. Ensure that it's configured with your credentials.
  • Use the following command to verify the installation:
aws --version
Screenshot-2024-08-23-164128

Now configure aws credentials by using aws configure command

aws configure
Screenshot-2024-08-23-111911

Step 2: Creating a Queue

Standard Queue:

aws sqs create-queue --queue-name MyStandardQueue
Screenshot-2024-08-23-164218

FIFO Queue:

aws sqs create-queue --queue-name MyFifoQueue.fifo --attributes FifoQueue=true
Screenshot-2024-08-23-164330

Step 3: Listing Queues

To list all queues:

aws sqs list-queues
Screenshot-2024-08-23-164504

Step 4: Sending a Message to a Queue

Standard Queue:

aws sqs send-message --queue-url <QueueURL> --message-body "This is a test message"
Screenshot-2024-08-23-164653

FIFO Queue:

aws sqs send-message --queue-url <QueueURL> --message-body "This is a test message" --message-group-id "group1"
Screenshot-2024-08-23-165402

Step 5: Receiving Messages

To receive a message from a queue:

aws sqs receive-message --queue-url <QueueURL>
Screenshot-2024-08-23-165617

Step 6: Deleting a Message

Once processed, delete the message from the queue:

aws sqs delete-message --queue-url <QueueURL> --receipt-handle <ReceiptHandle>
Screenshot-2024-08-23-165953

Step 7: Managing Dead-Letter Queues (DLQ)

To create a DLQ and associate it with an existing queue:

aws sqs set-queue-attributes --queue-url <QueueURL> --attributes RedrivePolicy='{"deadLetterTargetArn":"<DLQArn>","maxReceiveCount":"5"}'
Screenshot-2024-08-23-170833

For Standard Queue:

Screenshot-2024-08-23-171013

Step 8: Deleting a Queue

To delete a queue:

aws sqs delete-queue --queue-url <QueueURL>
Screenshot-2024-08-23-171423

Now we can check list of queues by using following command

aws sqs list-queues
Screenshot-2024-08-23-171622

Here we see all queue list was deleted successfully

Conclusion

Amazon SQS is an effective tool for building scalable, decoupled, and fault-tolerant systems. By using SQS, you can efficiently manage communication between different parts of your application, ensuring that messages are delivered and processed reliably.

With the AWS CLI, you can create and manage queues, send and receive messages, and set up advanced features like Dead-Letter Queues (DLQs) for handling unprocessable messages. Whether you’re working with high-throughput workloads using Standard Queues or need strict message ordering with FIFO Queues, SQS offers the flexibility and reliability needed for modern applications.

This document introduced the most important AWS CLI commands to work with SQS. All descriptions of the sections were clear, and many examples were provided. With this knowledge of the commands, the use of SQS can be optimized, resulting in applications that are able to perform better and become more resilient. Once more, scripting and automatization of tasks via the CLI increase the potential for handling SQS powerfully and effectively


Next Article
Article Tags :

Similar Reads