Amazon EBS Volume with CLI Tools – The Whole Enchilada

Amazon EBS Volume with CLI Tools – The Whole Enchilada


In our previous post “Install Amazon CLI Tools”, we showed how to install Amazon CLI tools which will open doors to different ways of managing Amazon resources that can be scripted and automated, even without going to Amazon web console. As part of our client engagement, we have scripted and automated the EBS lifecycle that enabled us to quickly provision the EBS volumes. Amazon EBS is second in popular to EC2, and this post will show how to create, attach, mount, detach, and delete an EBS volume from/to an EC2 instance with CLI tools.

Use Case

This use case shows how to create, attach, mount, detach, and delete an EBS volume to an EC2 instance using CLI tools. The prerequisite is to have Amazon CLI tools set up ready and working.

What we need to do:

  • Create EBS Volume
  • Attach EBS Volume to an EC2 Instance
  • Mount EBS Volume to an EC2 Instance
  • Detach EBS Volume from an EC2 Instance
  • Delete EBS Volume


Create EBS Volume:

We will create a 8 GB General Purpose (SSD) EBS volume in the availability zone us-west-2b and region us-west-2

EBS volume

Attach EBS Volume to an EC2 Instance:

Once the EBS volume is created, it should be attached to an EC2 instance so that it can be mounted later. It is important to note that attaching an EBS to an instance doesn’t make it ready to use without mounting the device to the instance. Attaching an EBS volume indicates that this volume is no longer available for use until it has been detached from the attached instance.

In the  command given below, provide the EBS volume id which can be retrieved from the above command. It shows after VOLUME and EC2 instance id as the id that we would like the volume to be attached. The output of the command as shown in the screenshot given below, shows the device name that is important to note to use it later for mounting. The device name “/dev/sdh” is highlighted and this could vary depending on the region, volume size, and many other parameters.

EBS to EC2

Mount EBS Volume to an EC2 Instance:

As indicated before, attaching an EBS volume to an EC2 instance doesn’t make it ready to use untill the device is mounted to the instance. This is little tricky and takes more time compared to other steps, and this is the step that might go wrong sometimes.

  • Find Device Name:

Use the lsblk command to view the disk devices available and their corresponding mount points. For the new volume that was attached to the instance, the mount directory will not be available till we mount the volume as shown in the  screenshot given below.


  • Create File System:

New volumes are raw block devices and can’t be used until a specific file system is created on them. It’s important to run this step if the EBS volume is created from a snapshot, as this step will wipe out all the data, if a new file system is created on them. The  command given below shows, how to create ext4 file system on the volume.

Sudo file
sudo mkfs

  • Create Mount Directory & Mount the Volume:

It’s time to create the mount directory of our choice and mount the volume to the created directory so that we can start using them.

mount directory

  • Persist the Mount:

It’s important to persist the mount details on the EC2 instance so that the volume will be automatically mounted every time the instance is rebooted. To do this, an entry needs to be added to the fstab file.

persist the mount

If the above command doesn’t thrown any errors then the entry was added successfully and the volume will be mounted for every instance restart.

  • Verify:

The volume is ready to use and can be verified by touching a file on the new mount directory /data


Detach EBS Volume from an EC2 Instance:

  • Un-mount the Device:

It’s time to detach the volume and make it available for use to other EC2 instance or can be deleted. However, before detaching the volume, it must be un-mount from the instance it’s been mounted to and then detach it. Change to a different directory than the mount directory for error-free un-mount process. Once the un-mount is done, the device will not be displayed as shown in the below screenshot.


  • Check Volume still attached:

Even though the volume is un-mounted from the instance, it is still attached to it and not available for use to any other instances. This can be verified by executing the following command and the screenshot shows the volume is still attached to the instance. The highlighted markers in the screenshot shows the instance Id and the BLOCKDEVICE with EBS Volume id that was attached before.

volume attached

  • Detach Volume:
    detach volume
  • Verify Volume Detached:

With the above process, the EBS volume is now actually detached from the instance that it was attached to before. This can be verified by re-running the describe command as did in the last before step and the BLOCKDEVICE with the volume id “vol-acca64ad “ is no longer attached.

Verify volume detached

Delete EBS Volume:

The detached volume can be either re-attached and mounted to a different instance or deleted from the account if it is not longer needed. The below web console shows the volume that was created and detached, and the status shows it to as “available”. We can safely delete the volume if it is no longer needed using delete volume command.

delete EBS volume


Refresh the web console to verify whether the EBS volume is deleted or not as shown in the below screenshot where the EBS volume no longer exists.



  • Creating, Attaching, Mounting, Detaching, and Deleting Amazon EBS volume is a long process and error-prone. However, the Amazon CLI tools provide some control over this and the whole lifecycle of EBS volume can be managed without ever going to Amazon web console.
  • With just one CLI tool, it’s possible to control multiple AWS services from the command line and automate them through scripts.


4409 Views 1 Views Today