Creating an AMI from an EC2 instance with mounted volumes

amazon ec2amazon-amiamazon-ebsamazon-web-services

I have an EBS-backed instance with a 2nd mounted volume (I followed the steps in Eric's Article)

I then created an AMI from that instance, and ran an instance from that AMI.

That 2nd instance then created two volumes.

Is there a way to control how the volumes associated with with an AMI are created, or is the solution just to unmount the 2nd volume before creating the AMI?

Best Answer

The ec2-create-image command line tool has a --block-device-mapping option to define the volumes associated with new instances.

http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-CreateImage.html

In your specific case, you may want to suppress the automatic creation and attaching of the database volume by specifying something like:

ec2-create-image --block-device-mapping /dev/sdh=none [...]

This will let you create and attach your own volumes to the new instances.

Alternatively, you could specify that an empty volume be created an attached when a new instance is started:

ec2-create-image --block-device-mapping /dev/sdh=:20:false [...]

When you run an instance, you could override the size of that volume using the --block-device-mapping option to ec2-run-instances.

It's also worth learning about the delete-on-termination flag for --block-device-mapping so that you understand how to control whether your second volume should automatically be deleted when the instance is terminated.