How to Send Messages to an SQS Queue with AWS CLI
Introduction
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that makes it easy to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message-oriented middleware, and empowers developers to focus on differentiating work.
In this lesson, we will learn how to send messages to an SQS queue with AWS CLI. We will cover the basics of SQS, how to create an SQS queue, and how to send messages to the queue using the AWS CLI.
What is Amazon SQS?
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that makes it easy to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message-oriented middleware, and empowers developers to focus on differentiating work.
SQS offers two types of message queues: standard and FIFO (First-In-First-Out). Standard queues provide best-effort ordering which ensures that messages are generally delivered in the same order as they are sent. FIFO queues guarantee that messages are delivered in the same order as they are sent.
Creating an SQS Queue
Before we can send messages to an SQS queue, we need to create the queue. To create an SQS queue, we can use the AWS CLI.
First, we need to create a policy that allows us to access the queue. We can do this by creating a JSON file with the following contents:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:SendMessage",
"sqs:ReceiveMessage",
"sqs:DeleteMessage"
],
"Resource": "arn:aws:sqs:<region>:<account-id>:<queue-name>"
}
]
}
Replace <region>
, <account-id>
, and <queue-name>
with the appropriate values for your environment.
Next, we can create the queue using the aws sqs create-queue
command.
aws sqs create-queue --queue-name <queue-name> --attributes '{"Policy": "<policy-json>"}'
Replace <queue-name>
and <policy-json>
with the appropriate values for your environment.
Sending Messages to an SQS Queue
Now that we have created an SQS queue, we can send messages to it. To send a message to an SQS queue, we can use the aws sqs send-message
command.
aws sqs send-message --queue-url <queue-url> --message-body <message-body>
Replace <queue-url>
and <message-body>
with the appropriate values for your environment.
Receiving Messages from an SQS Queue
We can also receive messages from an SQS queue. To receive a message from an SQS queue, we can use the aws sqs receive-message
command.
aws sqs receive-message --queue-url <queue-url>
Replace <queue-url>
with the appropriate value for your environment.
Deleting Messages from an SQS Queue
Finally, we can delete messages from an SQS queue. To delete a message from an SQS queue, we can use the aws sqs delete-message
command.
aws sqs delete-message --queue-url <queue-url> --receipt-handle <receipt-handle>
Replace <queue-url>
and <receipt-handle>
with the appropriate values for your environment.
Conclusion
In this lesson, we learned how to send messages to an SQS queue with AWS CLI. We covered the basics of SQS, how to create an SQS queue, and how to send, receive, and delete messages from the queue using the AWS CLI. With this knowledge, you should be able to easily integrate SQS into your applications.