This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
If you are not familiar with AWS Simple Queue Service (SQS), check out my article Amazon Web Services (AWS) - Getting Started with Simple Queue Service (SQS).
The aws sqs list-queues command can be used to list your queues.
~]$ aws sqs list-queues
{
"QueueUrls": [
"https://queue.amazonaws.com/123456789012/my-first-queue"
]
}
Likewise, the aws sqs get-queue-url command can be used to get the URL of your queue.
~]$ aws sqs get-queue-url --queue-name my-first-queue
{
"QueueUrl": "https://queue.amazonaws.com/123456789012/my-first-queue"
}
And then the aws sqs receive-message command can be used to get the message from the queue. The body is "hello". This will not delete the message in the queue. Instead, aws sqs delete-message can be used to delete the message from the queue.
~]$ aws sqs receive-message --queue-url https://queue.amazonaws.com/123456789012/my-first-queue
{
"Messages": [
{
"Body": "hello",
"ReceiptHandle": "AQEBqy3nHXKLjlXFCw7ZMky0z6bMgEG0MvvnNBLnQ/1drFX+9Oqh291bmiu3AqQjKni+REeYhyidtW5m+blD5WDA66dqfmGsk6q79jfrZf5GjNwgWU7FNJlm6//Sw21HdezqygzlM4slAQ54URQ9CqaTTUSmjWK6rwPuXrG1ULswGdUzp2ZNtxvSUy1km+nLy7Yp9rGhwUAX3vJlAZAGKKvce6owT/Tnf/uSi68dpv91mJH6LR23QUOdnBk8Bgl1v1SF1QkIo+DiMhY3/IoSu9FpnCZj3rSbafY32FcTLmqfctuVGAJnU9mkNRQuCMATFoSVkkjAgOrKjUZaQMZ6eyNdIS+l67N2QNbUpgsBBQtLaGg8CBHIkGCGJkKV+/ekgUXWj6/godVCkgOwo/hEeZeegQ==",
"MD5OfBody": "5d41402abc4b2a76b9719d911017c592",
"MessageId": "5508026b-e650-45d8-8482-8d13623d6a6b"
}
]
}
If you re-run your Python script multiple times without waiting between requests, the first request a first message in the queue (assuming there is at least one message in the queue), but subsequent requests made 30 seconds or less after the first request will not return any messages in the queue due to the visibility timeout. By default, a queues visibility timeout will be set to 30 seconds, which means that after you submit a request to receive a message in the queue, the message will be flagged as acknowledged for 30 seconds. If you simply wait 30 seconds (or longer), then the same message should be returned.Or, you can adjust the visibility timeout of the queue.
Did you find this article helpful?
If so, consider buying me a coffee over at