Skip to content

Instantly share code, notes, and snippets.

@camilb
Created September 20, 2017 10:26
Show Gist options
  • Save camilb/7b2110d50e990106a4bf598e7bd62d65 to your computer and use it in GitHub Desktop.
Save camilb/7b2110d50e990106a4bf598e7bd62d65 to your computer and use it in GitHub Desktop.
Move SQS messages between regions
#!/usr/bin/env python
import json
import time
import argparse
import boto.sqs
from termcolor import cprint
parser = argparse.ArgumentParser(description="Migrate messages from SQS queues.")
parser.add_argument('-s', '--src', required=True,
help='Name of the source queue.')
parser.add_argument('-d', '--dst', required=True,
help='Name of the destination queue.')
parser.add_argument('--sregion', default='eu-central-1',
help='The AWS region of the source queue (default: \'eu-central-1\').')
parser.add_argument('--dregion', default='us-east-1',
help='The AWS region of the destionation queue (default: \'us-east-1\').')
args = parser.parse_args()
aws_src_region = args.sregion
aws_dst_region = args.dregion
src_queue_name = args.src
dst_queue_name = args.dst
sconn = boto.sqs.connect_to_region(aws_src_region)
dconn = boto.sqs.connect_to_region(aws_dst_region)
src_queue = sconn.get_queue(src_queue_name)
dst_queue = dconn.get_queue(dst_queue_name)
while True:
messages = src_queue.get_messages(10)
for src_message in messages:
dst_message = boto.sqs.message.RawMessage()
print 'Processing message '+src_message.id
msg_body = src_message.get_body()
dst_message.set_body(msg_body)
dst_queue.write(dst_message)
src_queue.delete_message(src_message)
if len(messages) <= 0:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment