
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).
get_queue_attributes with AttributeNames=['ApproximateNumberOfMessages'] can be used to get the approximate count of messages in a queue.
import boto3
client = boto3.client('sqs')
get_queue_url_response = client.get_queue_url(QueueName='my-first-queue')
print(f"queue URL = {get_queue_url_response['QueueUrl']}")
get_queue_attributes_response = sqs_client.get_queue_attributes(
QueueUrl=get_queue_url_response['QueueUrl'],
AttributeNames=['ApproximateNumberOfMessages']
)
print(get_queue_attributes_response )
client.close()
Which should return something like this. Notice in this example that ApproximateNumberOfMessages is 123.
{'Attributes':
{'ApproximateNumberOfMessages': '123'},
'ResponseMetadata': {
'RequestId': 'b80ac6ed-0aa8-5156-85f2-a62272b3e694',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': 'b80ac6ed-0aa8-5156-85f2-a62272b3e694',
'date': 'Fri, 20 Jun 2025 14:53:15 GMT',
'content-type': 'text/xml',
'content-length': '123',
'connection': 'keep-alive'
},
'RetryAttempts': 0}
}
You probably just want to get the ApproximateNumberOfMessages.
import boto3
client = boto3.client('sqs')
get_queue_url_response = client.get_queue_url(QueueName='my-first-queue')
print(f"queue URL = {get_queue_url_response['QueueUrl']}")
get_queue_attributes_response = sqs_client.get_queue_attributes(
QueueUrl=get_queue_url_response['QueueUrl'],
AttributeNames=['ApproximateNumberOfMessages']
)
print(get_queue_attributes_response ['ApproximateNumberOfMessages'])
client.close()
Did you find this article helpful?
If so, consider buying me a coffee over at