In this post I share an experiment to create an EBS volume, to attach it to an EC2 instance, to mount it in the instance, to put a file on it, to unmount it, and to detach it. Afterwards the volume will be mounted in another instance (while the first instance has been terminated, because attaching volumes to different instances at the same time is impossible).

I followed the instructions given in the Elastic Block Storage Feature Guide.

Starting an Instance

Let’s see which AMIs are available:

I launch ami-c6c622af with Elasticfox. Let’s check the status of the instance with the command line tools:

Important to note for later is the availability zone in which the instance is running, because volumes can ony be attached to instances when they live in the same availability zone.

Create the Volume

Create a volume of 1 GB in the same availability zone in which the instance resides:

Check the status of the volume:

The volume is available now. Time to use it!

Attaching the Volume

Attach the newly created volume as device /dev/sdh to the running instance:

The command returns saying that the volume is attaching. Let’s check the status:

While the volume was “available” and “attaching” before, now it is “in-use” and “attached”.

Formatting the Volume

Open another terminal. Connect to the instance via ssh:

Looking at the contents of /dev reveals that the volume is available as device “sdh”:

Since a new volume is not formatted, we do that first:

Mounting the Volume

Finally, the volume is ready to be mounted in the instance:

Let’s check whether everything is as expected:

That looks okay.

Put a file on the volume

Using vi, I created a file named “readme” with this contents:

Unmounting the Volume

Before we stop the instance, we have to unmount the volume. From the Elastic Block Storage Feature Guide:

A volume must be unmounted inside the instance before being detached. Failure to do so will result in damage to the file system or the data it contains.

Remember to cd out of the volume, otherwise you will get an error message “umount: /mnt/data-store: device is busy”

Detach the Volume

From the Feature Guide: An Amazon EBS volume can be detached from an instance by either explicitly detaching the volume or terminating the instance. Let’s do it by explicitly detaching it:

Soon the status of the volume changes form “detaching” to “available”:

Mounting the Volume in Another Instance

Now do all the steps over again to start a new image and mount the volume. Because the volume resides in availability zone “us-east-1c” and instances and volumes have to live in the same availability zone, we have to launch the instance in “us-east-1c”.

Now start anoher terminal to connect to the instance:

The file we created earlier was on the volume and we could read it. This proves that we can share volumes among instances.

To clean up: