We build Web & Mobile Applications.
A quick set of notes on how to create a bootable EBS snapshot from a running EC2 instance - for example, an instance that has been started from an S3 backed AMI.
We’ve had to do this a few times over the last few months - for the benefit of others, we’ve outlined how we currently do it - this is based on a number of articles that were surfacing at the time of our research, but I don’t have the links to hand. If there’s a better way out there feel free to jump in!
Create an EBS volume (if converting from an S3 backed AMI then create it as 10Gb in size)
Attach the EBS volume to your running instance
Stop running services that may be locking files, etc, such as MySQL
At this point, you’re ready to copy the contents of your root drive onto the EBS volume. A couple of additional points here - it makes sense to stop any services possible to make the transfer as clean as possible. One of our readers has suggested an alternative approach to dd - I’ve included it here as an option for completeness.
You have two options for copying the disk:
Option 1 - Use dd to copy the root drive to the EBS volume
# Replace SOURCE with the boot volume, e.g. /dev/sda1 and TARGET with the EBS volume, e.g. /dev/sdj
dd bs=65536 if=SOURCE of=TARGET
fsck TARGET
mkdir /migrate
mount TARGET /migrate
Option 2 - Use rsync
I suspect with this route you’d need to exclude any directories used for mounting other volumes, otherwise your rsync will run out of space on the target device.
mkfs.ext3 TARGET
mount TARGET /migrate
rsync -ax --progress / /migrate
rm -rf /migrate/migrate
At this point, you can make any necessary modifications to the fstab to allow your EBS based AMI to boot - the file can be found in /migrate/etc/fstab
umount TARGET
Create a snapshot of the EBS volume
Register the snapshot as an AMI by running
ec2-register -n NAME --architecture ARCH --block-device-mapping /dev/sda1=SNAPSHOT:SIZE:false
There are a number of options here - some of which you can set when starting the instance if you prefer.
The block device mapping paramaters set the EBS snapshot to use as the boot device, the second parameter sets the size to 15Gb and final parameter sets the delete-on-termination attribute to false.
Run an instance based on the newly registered AMI - if you’ve set the EBS volume size to anything greater then 10Gb then expand the filesystem to the size of the device.
More details, as usual, can be found in the command line reference.