Skip to content

Instantly share code, notes, and snippets.

@j0e1in
Last active August 2, 2020 17:47
Show Gist options
  • Save j0e1in/7d6d907133925d6789989bfb41eadc99 to your computer and use it in GitHub Desktop.
Save j0e1in/7d6d907133925d6789989bfb41eadc99 to your computer and use it in GitHub Desktop.
Copy AWS SQS queue messages to another queue.
from tqdm import trange
import argparse
import boto3
parser = argparse.ArgumentParser(description="Migrate messages from SQS queues.")
parser.add_argument('--src', required=True, help='Name of the source queue.')
parser.add_argument('--dst', required=True, help='Name of the destination queue.')
parser.add_argument('--batches', help='Number of batches to process', default=1, type=int)
parser.add_argument('--batch-size', help='Number of messages in a batch', default=10, type=int)
args = parser.parse_args()
sqs = boto3.resource('sqs')
src = sqs.get_queue_by_name(QueueName=args.src)
dst = sqs.get_queue_by_name(QueueName=args.dst)
def filter_msg(msg):
return True # Add your conditions here to filter messages that will be copied over
for _ in trange(args.batches):
for msg in src.receive_messages(MaxNumberOfMessages=args.batch_size):
if filter_msg(msg.body):
print(f"Copying message {msg.body}")
dst.send_message(MessageBody=msg.body)
# msg.delete() # uncomment this if you want to delete copied messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment