Updated post here.
When creating an Amazon Machine Image (AMI) where there are additional volumes added, these volumes are typically not set to delete on termination. This means that after you spin through 5-10 instances based on this AMI there will remain a large number of volumes left over that were not deleted when the instances were terminated. This may result in higher usage costs and a more difficult time managing images as there will be large number of volumes to sort and filter through.
The following procedure describes how to resolve this issue through setting the deleteOnTermination flag on the AWS instnace prior to creating an AMI so that all volumes will be cleaned up properly.
Note that the operations here should be performed on a running instance either before the AMI is first cut or an instance that is spun up based on the AMI in question. This instance will then be used to create an AMI that will be used for future operations.
– Setup command line environment to work with EC2/AWS without the need for password prompts and any authentication. This requires the installation of the ec2-api-tools and related keys and environment variables.
1. List volumes associated with instance:
ec2-describe-instance-attribute -v -b instanceid BLOCKDEVICE /dev/sda1 vol-XXXXXXXX 2011-06-06T14:59:20.000Z BLOCKDEVICE xvdg vol-XXXXXXXX 2011-06-06T16:58:13.000Z BLOCKDEVICE xvdm vol-XXXXXXXX 2011-06-06T16:59:07.000Z BLOCKDEVICE xvdl vol-XXXXXXXX 2011-06-06T16:59:24.000Z BLOCKDEVICE xvdf vol-XXXXXXXX 2011-06-06T16:59:39.000Z REQUEST ID XXXXXXXX-a12a-4df5-b944-XXXXXXXX
# For each device above, modify the device attributes to set delete on termination:
ec2-modify-instance-attribute -b xvdg=vol-XXXXXXXX:true instanceid ec2-modify-instance-attribute -b xvdm=vol-XXXXXXXX:true instanceid ec2-modify-instance-attribute -b xvdl=vol-XXXXXXXX:true instanceid ec2-modify-instance-attribute -b xvdf=vol-XXXXXXXX:true instanceid
I received an error each time I ran this command that turned out successful anyway, which is why the verification step below.
Unexpected error: java.lang.ClassCastException: com.amazon.aes.webservices.client.InstanceBlockDeviceMappingDescription cannot be cast to com.amazon.aes.webservices.client.InstanceBlockDeviceMappingResponseDescription at com.amazon.aes.webservices.client.cmd.Outputter.outputInstanceAttribute(Outputter.java:664) at com.amazon.aes.webservices.client.cmd.ModifyInstanceAttribute.invokeOnline(ModifyInstanceAttribute.java:149) at com.amazon.aes.webservices.client.cmd.BaseCmd.invoke(BaseCmd.java:795) at com.amazon.aes.webservices.client.cmd.ModifyInstanceAttribute.main(ModifyInstanceAttribute.java:269)
# Verify that all volumes are setup correctly:
ec2-describe-instance-attribute -v -b instanceid | egrep deleteOnTermination true true true true true
Why would somebody be worried about this issue:
When working with continuous integration, the intent is to automate everything. Part of that automation process with the work I have been doing lately is to create AMIs for each machine/server/instance function that will be programattically deployed and configured to remove any human intervention from the process.
Ideally this entire process would be based on Amazon official AMIs but that is further down the road. At this point I am creating and maintaining custom AMIs for each machine function.