Using CLI send and receive messages from SQS Queues
Services Covered
SQS
Lab description
Learning Objectives
- Creating queue and sending messages to it
- Using Cloud9 and CLI to interact with SQS
Lab date
03-12-2021
Prerequisites
- AWS account
Lab steps
- Navigate to SQS Dashboard and create a standard queue, keep all the defaults.
- Go to Send and receive messages and send some message, Add some attributes.
- Click Poll for Messages. You message will be on the received list. Then delete it.
- Navigate to Cloud9. Click on Open IDE. In your Cloud9 Terminal, enter the
sqs list-queues
command to display the URL of your SQS queueaws sqs list-queues
- Issue the following command to display only the QueueUrls field in text format:
aws sqs list-queues --output text --query "QueueUrls"
- Store the queue URL in a shell variable and send a message to the queue using the
send-message
command:# Store the Queue URL in a shell variable named queue_url # The command is surrounded with $(...) to store the result of the command queue_url=$(aws sqs list-queues --output text --query "QueueUrls") # Send a message to the queue (use $ to substitute the value stored in the variable) aws sqs send-message --queue-url $queue_url --message-body "I'm an SQS message from the CLI"
- In your Cloud9 terminal, enter the following command:
aws sqs receive-message --queue-url <QUEUE URL>
- In terminal enter the following command:
aws sqs delete-message --queue-url $queue_url --receipt-handle <YOUR_RECEIPT_HANDLE>
- Delete the queue in SQS.