Created
June 14, 2019 18:14
-
-
Save rajmayank/2869b0d2a9c29deeb2f5c530a0e7f162 to your computer and use it in GitHub Desktop.
AWS SQS - Move from the Deadletter Queue (DLQ) to Source Queues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
import uuid | |
sqsClient = boto3.client('sqs', region_name='us-east-1') | |
# Source Queue (eg. DLQ) | |
srcQueueUrl = '' | |
# Destination Queues | |
destQueue = [ | |
'', | |
'', | |
] | |
toQueueIndex = 0 | |
while True: | |
recResp = sqsClient.receive_message( | |
QueueUrl=srcQueueUrl, | |
MaxNumberOfMessages=10, | |
) | |
if 'Messages' not in recResp: | |
break | |
sendResp = sqsClient.send_message_batch( | |
QueueUrl= destQueue[toQueueIndex], | |
Entries=[{ 'MessageBody': x['Body'], 'Id': str(uuid.uuid4()) } for x in recResp['Messages']] | |
) | |
delResp = sqsClient.delete_message_batch( | |
QueueUrl=srcQueueUrl, | |
Entries=[{ 'Id': str(uuid.uuid4()), 'ReceiptHandle': x['ReceiptHandle'] } for x in recResp['Messages'] ] | |
) | |
print(len(recResp['Messages']), 'messages moved to: ', destQueue[toQueueIndex]) | |
toQueueIndex += 1 | |
if toQueueIndex == len(destQueue): | |
toQueueIndex = 0 | |
print("===============================") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment