Skip to content

Instantly share code, notes, and snippets.

@rajmayank
Created June 14, 2019 18:14
Show Gist options
  • Save rajmayank/2869b0d2a9c29deeb2f5c530a0e7f162 to your computer and use it in GitHub Desktop.
Save rajmayank/2869b0d2a9c29deeb2f5c530a0e7f162 to your computer and use it in GitHub Desktop.
AWS SQS - Move from the Deadletter Queue (DLQ) to Source Queues
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