While instances backed with EBS-root volumes do cost a bit more than the equivalent instance-store images (there is a cost for I/O), they have certain advantages – persistence and ease of replacement being notable. This article will focus on the latter.
As a quick side note before proceeding, it is possible to add ephemeral storage to an EBS-root instance, although, it must be explicitly specified and will not be included by default. See the EC2 documentation for more information.
I recently ran into a scenario where an EBS-root instance had been shut down and the snapshots backing its AMI were outdated. Luckily, I had previously ensured that all the EBS volumes attached to the instance would persist after termination. In many cases, the root device of an EBS-root instance is set to automatically be deleted on termination of the instance.
Setting the DeleteOnTermination
flag to false, will cause a volume to persist after instance termination, and can be done using the ec2-modify-instance-attribute
command line tool in the following manner:
ec2-modify-instance-attribute INSTANCE_ID -b "MOUNT_POINT=VOLUME_ID:false"
For example:
ec2-modify-instance-attribute i-a1b2c3d4 -b "/dev/sda1=vol-9f8e7d6c:false"
You can determine the current value of the DeleteOnTermination
flag, by looking at the verbose output of ec2-describe-instance-attribute
. Unfortunately, the formatted output does not list the value of this flag.
ec2-describe-instance-attribute INSTANCE_ID -v -b
Do not forget to export your private key (EC2_PRIVATE_KEY
) and certificate (EC2_CERT
) environment variables (or set them (-K and -C respectively) in the above commands).
As a side note, using ec2-modify-instance-attribute
it is also possible to change the instance type (-t). Simply stop the instance, run the command, and start the instance (this, does not apply to spot instances, unfortunately, which cannot be stopped).
Back to the scenario above, the problem is that you cannot create an instance from an EBS volume. If one is working with spot instances, the problem is compounded by the fact that these instances cannot be stopped. The solution is as follows:
- Launch an on-demand EBS-root instance.
- Stop the instance
- Detach the attached EBS volumes (including the root volume)
- Attach the desired EBS volumes to the correct mount points
- Start the instance
From here, you can, of course, create an AMI from the running instance (which will automatically create snapshots as well) and when launched, will create new EBS volumes from those snapshots. Furthermore, since pre-existing volumes were attached, they should already have the DeleteOnTermination flag set to false, and the AMI created from this instance will inherit the same settings.
Thanks for the very useful hint. I wanted to create an instance using an existing volume as the root, and couldn’t figure out how to do it — that is until I read this post.