
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).
You will need the URL of the queue you want to put messages on. This can be done using list_queues or get_queue_url. And then send_message can be used to put/publish a message on the queue. If publishing to a Standard Queue, you do not need to include MessageGroupId and MessageDeduplicationId.
#!/usr/bin/python3
import boto3
client = boto3.client('sqs')
response = client.get_queue_url(
QueueNamePrefix='my-first-queue',
QueueOwnerAWSAccountId='123456789012'
)
print(f"queue URL = {response['QueueUrls']}")
send_message_response = client.send_message(
QueueUrl=response['QueueUrls'],
MessageBody='Hello World'
)
print(send_message_response)
client.close()
If you are publishing to a First In First Out (FIFO) queue, you will need to also include MessageGroupId and MessageDeduplicationId.
#!/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']}")
send_message_response = client.send_message(
QueueUrl=response['QueueUrl'],
MessageGroupId='my-message-group-id',
MessageDeduplicationId='my-message-duplication-id',
MessageBody='Hello World'
)
print(send_message_response)
client.close()
Let's say you publish a message to a First In First Out (FIFO) queue, and then immediately try to publish another message with the same MessageDeduplicationId. This second message will not be put on the queue because by default messages put on the queue with the same MessageDeduplicationId in a 5 minute period will be acknowledged but not put on the queue. If this is going to be a problem for you, and you need to publish multiple messages in a short period of time, you could use UUID to give each message its own, unique MessageDeduplicationId.
#!/usr/bin/python3
import boto3
import uuid
client = boto3.client('sqs')
response = client.get_queue_url(
QueueName='my-first-queue',
QueueOwnerAWSAccountId='123456789012'
)
print(f"queue URL = {response['QueueUrl']}")
send_message_response = client.send_message(
QueueUrl=response['QueueUrl'],
MessageGroupId='my-message-group-id',
MessageDeduplicationId=str(uuid.uuid4()),
MessageBody='Hello World'
)
print(send_message_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.get_queue_url(
QueueName='my-first-queue',
QueueOwnerAWSAccountId='123456789012'
)
except Exception as exception:
print(exception)
sys.exit(1)
else:
print(f"queue URL = {response['QueueUrl']}")
try:
send_message_response = client.send_message(
QueueUrl=response['QueueUrl'],
MessageBody='Hello World'
)
except Exception as exception:
print(exception)
sys.exit(1)
else:
print(send_message_response)
try:
client.close()
except Exception as exception:
print(exception)
Something like this should be returned.
{
'MD5OfMessageBody': 'b10a8db164e0754105b7a99be72e3fe5',
'MessageId': '681f4a2f-a713-42fe-8be3-7df263e2991c',
'ResponseMetadata': {
'RequestId': '2331a234-909e-501c-a65c-fc48b742307c',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '2331a234-909e-501c-a65c-fc48b742307c',
'date': 'Tue, 26 Mar 2024 01:32:12 GMT',
'content-type': 'text/xml',
'content-length': '378',
'connection': 'keep-alive'},
'RetryAttempts': 0
}
}
More realistically, messages will typically be in a structured format, such as a dictionary.
send_message_response = client.send_message(
QueueUrl=response['QueueUrl'],
MessageBody='{"foo": "hello", "bar": "world"}'
)
client.close()
Did you find this article helpful?
If so, consider buying me a coffee over at