AWS SQS Boto3 Basics

I’ve spent today figuring out the ins and outs of SQS for use as a provisioning queue and to go along with the previous Boto3 posts I decided to share base functionality of how you can connect to a Queue, Send a Message, Read a Message and Delete said Message which is most of what you’ll ever need to do when using SQS.

Create a Boto3 Session

from boto3.session import Session

# Create the Boto3 Session
session = Session(
client = session.client('sqs')

# Get the Queue URL
response = client.get_queue_url(
    QueueName='MyProvisionirengQueue' # Or the name of your SQS queue
url = response['QueueUrl']

Read SQS Messages

Here we are reading 1 message at a time from the queue (I have my listener as a daemon and want to process only 1 message at a time – its up to you on how you want to handle this).

See docs for options:

messages = client.receive_message(
if messages.get('Messages'):
    m = messages.get('Messages')[0]
    body = m['Body']
    receipt_handle = m['ReceiptHandle']
    do_something_with_msg_body(body) # I pass this to salt-cloud for provisioning, you can do whatever you want here

Send SQS Message

message = 'Hello world!'
response = client.send_message(

Delete SQS Message

It’s important to note that the receipt_handle is used when working with messages and that you need to use the most recent receipt handle returned from SQS when deleting a message.

response = client.delete_message(

For more information I recommend checking out the Boto3 documentation here:

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.