Yes, you CAN use attach a Linux EBS root volume to a different EC2 instance

By | August 7, 2012

At my current gig, we’re hosting our blog on Amazon EC2, at least until we have become so wildly successful that a single EC2 instance can’t keep up with our blog traffic.

We wanted to make sure we wouldn’t lose our blog in case of an EC2 failure, so we configured our (Linux) blog instance with an EBS root volume that is not deleted automatically when the instance is terminated. However, the question remained: exactly how would we recover from such a failure?

The most obvious solution is to simply launch a new, similarly configured instance with the old EBS volume as its root. Theoretically, the instance should simply boot up and seamlessly pick up where the old instance left off, albeit with a new IP address, unless you’ve assigned it an elastic IP.

Amazon seems to discourage using the EBS root volume of one instance on another, though they don’t explicitly say that it doesn’t work. Well, it does, at least for Linux instances. For all I know, this has already been written about by many other people, but I couldn’t find it quickly when I went looking, so in the spirit of making it easier to find for others in the future, here’s the story…

Note: When you’re launching the first instance, make sure you disable the Delete on Termination bit on its EBS root volume. Somewhat confusingly, if you just uncheck the box when editing the volume settings when launching the instance, that’s not good enough; you have to click the Save button after unchecking the box, or the change won’t actually be saved. Can you tell we got this wrong the first time? 🙂

To move a Linux EBS root volume from one instance to another:

  1. Stop or terminate the source instance, if it isn’t already stopped or terminated (but see above about the Delete on Termination bit, or you may be sorry!).
  2. Launch a new instance with a similar configuration to the old one. As far as I can tell, you cannot use the old EBS volume as the root here; you have to actually launch the new instance with its own root volume. It’s probably a good idea to disable the Delete on Termination bit for this instance as well, just to be cautious.
  3. Once the target instance has finished launching, stop it.
  4. Detach the newly created root volume from the target instance.
  5. Detach the old root volume from the source instance. If the source instance is terminated, it should already be detached; if it doesn’t look like it is, refresh your instance view. There’s no harm in trying to detach it if it’s already detached; you’ll just get an harmless error.
  6. Attach the old root volume to the target instance, using /dev/sda1 as its device name.
  7. Start the target instance, and it should boot up with no trouble.
  8. Confirm that the new instance is functioning as expected with the old EBS root.
  9. Adjust DNS records etc. to accommodate the IP address of the new instance, unless you’ve got an elastic IP assigned to it.
  10. Once you’re confident that everything is working as expected, remove the EBS volume you created in step 2, since it’s no longer needed.

Needless to say, your mileage may vary, test this out before you rely on it in production, etc.


Print Friendly, PDF & Email

Leave a Reply

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