Skip to content

Instantly share code, notes, and snippets.

@ajoyoommen
Created November 20, 2018 07:09
Show Gist options
  • Save ajoyoommen/e8e02713269b07eaaa7d9eb9718f5838 to your computer and use it in GitHub Desktop.
Save ajoyoommen/e8e02713269b07eaaa7d9eb9718f5838 to your computer and use it in GitHub Desktop.
This script will push data into a queue and poll from the queue until there are no more messages.
import boto3
import json
sqs = boto3.client('sqs', region_name='us-east-1')
queue = 'https://sqs.us-east-1.amazonaws.com/93423423423/boto-queue'
def push_to_queue(data, attrs):
return sqs.send_message(
QueueUrl=queue,
DelaySeconds=0,
MessageAttributes=attrs,
MessageBody = json.dumps(data)
)
def receive_messages():
print('Polling for messages:')
response = sqs.receive_message(
QueueUrl=queue,
AttributeNames=[
'All'
],
MaxNumberOfMessages=10,
MessageAttributeNames=[
'All'
],
VisibilityTimeout=30,
WaitTimeSeconds=0
)
if 'Messages' not in response:
return None
for message in response['Messages']:
handle = message['ReceiptHandle']
print('Received message: {}, attrs: {}\n'.format(message['Body'], message['MessageAttributes']))
sqs.delete_message(QueueUrl=queue, ReceiptHandle=handle)
receive_messages()
if __name__ == '__main__':
data = {
'AssociationToken': 'alksjdflkjsdklfjsdfjlaksjfl'
}
resp_create = push_to_queue(data, {
'Purpose': {
'DataType': 'String',
'StringValue': 'Create'
}
})
print('Pushed to sqs', resp_create)
resp_update = push_to_queue(data, {
'Purpose': {
'DataType': 'String',
'StringValue': 'Update'
}
})
print('Pushed to sqs', resp_update)
receive_messages()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment