
This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.
This also assumes you are familiar with Amazon Web Services (AWS) Simple Queue Service (SQS). If not, check out my article Amazon Web Services (AWS) - Getting Started with Simple Queue Service (SQS).
Here is the minimal boilerplate code without any error handling to list your SQS queues.
#!/usr/bin/python3
import boto3
client = boto3.client('sqs')
response = client.list_queues()
print(response)
client.close()
Here is a more practical example, with try/except/else error handling.
#!/usr/bin/python3
import boto3
import sys
try:
client = boto3.client('sqs')
except Exception as exception:
print(exception)
sys.exit(1)
try:
response = client.list_queues()
except Exception as exception:
print(exception)
sys.exit(1)
else:
print(response)
try:
client.close()
except Exception as exception:
print(exception)
Something like this should be returned.
{'QueueUrls':
['https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue'],
'ResponseMetadata': {
'RequestId': '5c0ae454-51d5-5de8-ac2c-ba2871c8b1f3',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '5c0ae454-51d5-5de8-ac2c-ba2871c8b1f3',
'date': 'Tue, 26 Mar 2024 00:59:09 GMT',
'content-type': 'text/xml',
'content-length': '330',
'connection': 'keep-alive'
},
'RetryAttempts': 0}
}
You probably just want to get the queue URLs.
#!/usr/bin/python3
import boto3
client = boto3.client('sqs')
response = client.list_queues()
for queue in response['QueueUrls']:
print(f"queue = {queue}")
client.close()
Which should return something like this.
queue = https://sqs.us-east-1.amazonaws.com/123456789012/my-first-queue
Or, you could use get_queue_url.
#!/usr/bin/python3
import boto3
client = boto3.client('sqs')
response = client.get_queue_url(
QueueName='my-first-queue',
QueueOwnerAWSAccountId='123456789012'
)
print(f"queue URL = {response['QueueUrl']}")
client.close()
Did you find this article helpful?
If so, consider buying me a coffee over at