How To Attach And Mount an EBS Volume To AWS EC2 Linux Instance

Step 1: EC2 console –> Volumes and create a new volume of your preferred size and type.

Step 2: Select the created volume, right click and select the “attach volume” option.

Step 3: Select the instance from the instance text box and complete attachment

Step 4: Login to the ec2 instance and list the available disks using the following command. The command will list the disk you attached to your instance.

lsblk

Step 5: Check if the volume has any data using the following command.

sudo file -s /dev/xvdf

If the above command output shows “/dev/xvdf: data”, it means your volume is empty.

Step 6: Format the volume to ext4 filesystem  using the following command.

sudo mkfs -t ext4 /dev/xvdf

Step 7: Create a directory of your choice to mount our new ext4 volume. I am using the name “data”

sudo mkdir /data

Step 8: Mount the volume to “data” directory using the following command.

sudo mount /dev/xvdf /data/

Step 9: cd into data directory and check the disk space for confirming the volume mount.

cd /data
df -h 

The above command would show the free space in the data directory.

By default on every reboot the  EBS volumes other than root volume will get unmounted. To enable automount, you need to make an entry in the /etc/fstab file.

1. Back up the /etc/fstab file
sudo cp /etc/fstab /etc/fstab.bak

2. Open /etc/fstab file and make an entry in the following format.
device_name mount_point file_system_type fs_mntops fs_freq fs_passno

For example,

/dev/xvdf       /data  ext4    defaults,nofail

3. Execute the following command to check id the fstab file has any error.

sudo mount -a
If the above command shows no error, it means your fstab entry is good.

Now, on every reboot the extra EBS volumes will get mounted automatically.

That’s how you mount and unmount EBS volumes in your ec2 instances.

Leave a Reply

Your email address will not be published. Required fields are marked *