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(
    aws_access_key_id='abcdefghijklmnop',
    aws_secret_access_key='abcdefghijklmnop',
    region_name='us-east-1',
)
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: 

http://boto3.readthedocs.org/en/latest/reference/services/sqs.html#SQS.Client.receive_message

messages = client.receive_message(
    QueueUrl=url,
    AttributeNames=['All'],
    MaxNumberOfMessages=1,
    VisibilityTimeout=60,
    WaitTimeSeconds=5
)
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(
    QueueUrl=url,
    MessageBody=message,
    DelaySeconds=0,
)

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(
    QueueUrl=url,
    ReceiptHandle=receipt_handle
)

For more information I recommend checking out the Boto3 documentation here:
http://boto3.readthedocs.org/en/latest/reference/services/sqs.html

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.