Moving EC2 instances across regions on Amazon Web Services (AWS)

the moveWe started using Amazon AWS at work for hosting and I landed up being the server admin. I've been stuck in terminal for more than 3 weeks and not touched a line of code (apart from turning debug on after this move to make sure the cache was cleared) and one awesome bash backup script.

After building a monster development box on AWS to host git (new application), SVN (old application), gitweb, a few other source reporting tools and more I started building out little EC2 instances for our application. This is where a few mistakes started creeping in.

Given that it was my first time ever using Amazon hosting and the, imo, poor docs I started out with an instance store. After reading some more and talking to colleagues we decided that EBS (Elastic Block Storage) was the way forward. Initiate the first move.

After a lot of web searching and help from some guys on the freenode #ubuntu-uk channel, I got the boxes on EBS.

A few days later we found out that to be in line with UK data protection laws, the websites for UK clients had to be within the EU. All the instances were in the US data centre, as it was the default option when I started creating them. Initiate the second move.

At this time we decided it would be a good idea to consolidate the companies hosting (across 3 hosts) and move everything to Amazon. Now I am sure there must be a very easy way to do this (and I have found ways since to reduce the number of steps), but this is the solution that I came up with. A mishmash of help from various sites around the web, and some friendly guys on irc.

The first step to the process is to backup the box being migrated, the 'old' server.

Now that the old box has been backed up, its time to create the new instance. This could be in another location or moving between EBS and instance store. Pick an image that matches what you need, but remember this method will completely overwrite the new install with the old so you cant change flavours of Linux etc.

When you have the new box up and running you can add IP addresses and star the backup migration to new instance from old instance.

Once that has finished copying (its pretty fast, I was moving about 1gig in less than 10 min) ssh to new instance and unzip the backup.

If you have moved locations your sources will be pointing to internal IP's for last region so that should be changed or you wont be able to update again. If you have just been relocating from the instance store to EBS this should not be needed.

This will require a bit of nano / vim foo if you want it done quick. You can either do it manually or use the commands below to get it done in seconds.

After that was sorted I was faced with a new error. Ubuntu was a bit confused about updates, some of this might not be relevant but is the issues I faced. Again this is from moving locations I believe. Skip this if you just changed to or from EBS in the same location.

The server may still spew errors out when you run the update, if it complains about keys do the next step. If it does not you were lucky (or I was unlucky).

The key that Ubuntu moans about looks like "0EC7E508BE09C571", we need to update / get this key so that it will work again. I found a blog that explained how to do it and this is what you need to do.

The only thing that seemed to break was MySQL, this was fixed with reinstalling it (make sure you have a backup of the database(s) before doing this). The reason I think this happened was due to the fact that I did not log in as root when doing the backup in the first place and some things did not get copied. If you followed this you might be OK, the error was related to a missing mysqlnd.sock file (or link)

Remember that your IP address would have changed due to being in a new location so you will need to wait for it to propagate if there are domains on it. You will also need to update anything like SSH config and services looking for the box on the old IP (internal or external). Once you are happy that your box is up and running in the new location you can go ahead and kill the old one. You might want to back it up to S3 first though.

TL;DR how to migrate Amazon EC2 boxes between locations and/or from one type to another (EBS → instance store)

Moving things about on Amazon

 

And a final bit for my fellow *nix n00bs, stop using ssh -p <port> <user>@<host> all the time and have a look at this, quick easy time saver.

I had some plans for doing some posts on setting up some servers in the cloud, but this seemed more interesting. Other ideas I've had included setting up the security for a hosted application across multiple servers. Tell me what you want to know about Amazon and ill see what I can do for you.

kvzon 22/4/11

May want to consider blueprint for server migrations. Works quite well.