Migrate AWS EBS Linux Instance from Region A to Region B

Recently I’ve had to migrate one of my AWS instances to another region, and unfortunately Amazon doesn’t provide an easy way to do this. I found a few ways to do it, but some of them didn’t work (notably, I had issues using rsync across regions. Data transferred ok, but I couldn’t connect to the new instance).

The method that did work for me was located at: http://www.slideshare.net/pleasebiteme/presentation-migrating-aws-ebs-backed-amis-between-regions-9465619 – I didn’t have any issues with this process, so it’s documented here for future reference.

Best thing about doing it this way is you don’t need to mount the volume on either the source or the destination server from the operating system, and there is no file system creation required on the destination volume.

Here are the simplified steps to do it, but if you’d like more info please visit the original source – linked above.

Set up the source instance:

1. Start up a micro instance in the source region
2. Create a snapshot of the instance you want to transfer
3. Create a volume from the snapshot
4. Attach the new volume to /dev/sdf
Note: If you’re using an Ubuntu version > 10, the volume may be renamed… beware of that.

Set up the destination instance:

5. Create a micro instance in the destination region, use the same key pair used with the source instance (AWS allows importing an existing keypair)
6. Create an empty volume and attach it to /dev/sdf
Note: If you’re using an Ubuntu version > 10, it may be renamed

Add the necessary SSH key to the source instance

7. SSH into the source instance, add the private key to the instance

vim default_keypair.pem;
...Paste your private key and save it
chmod 600 default_keypair.pem;

8. Test SSH from the source to the destination (this will add the destination’s IP to the known_hosts file)

ssh -i default_keypair.pem ubuntu@ec2-dest-ip-address;

Transfer the data from source to destination

9. Do the actual data transfer, this could take upwards of an hour to complete.

sudo su;
dd if=/dev/sdf | gzip -c -1 | ssh -i default_keypair.pem ubuntu@ec2-dest-ip-address "gunzip -c -1 | sudo dd of=/dev/sdf";

After the transfer is complete, something like the following will be output to your terminal:

16777216+0 records in
16777216+0 records out
8589934592 bytes (8.6 GB) copied, 2861.36 s, 3.0 MB/s
16777216+0 records in
16777216+0 records out
8589934592 bytes (8.6 GB) copied, 2937.77 s, 2.9 MB/s

Set up the new instance

10. Create a snapshot of the volume you just populated with data
11. Create an AMI from the snapshot
– make sure in the dialog you use the correct Architecture as the original AMI and give it a name.
– You should be able to use the default options for the rest.
NOTE: In the case that the same AMI doesn’t exist in the source region, start up a new AMI as close as possible to the source and copy the Ramdisk ID and Kernel ID from the running instance

Finish up / tidy up

12. Finally, start up a new instance from your custom AMI image and test that it works properly
13. Once you’ve verified the transfer went well, terminate your receiver and source instances if you don’t need them anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.