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:
https://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: https://boto3.readthedocs.org/en/latest/reference/services/sqs.html