How to attach and mount EBS volume on ec2 Linux instance on AWS

In this article, we will learn how we can attach and mount an EBS volume on an EC2 Linux instance on AWS. You might know that EC2 is nothing more than a flexible cloud of computing, which is a virtual computing environment (similar to a virtual machine in VMware). EBS (Elastic block store) is nothing more than an additional volume or disk that can be attached to EC2 (virtual compute) to set an additional mount point in Linux.

So let’s get started with the steps to attach and mount an EBS volume on an ec2 Linux instance on AWS

Step 1: Click on the ELASTIC BLOCK STORE–> Volumes button from the left panel in the EC2 Information Console.

Step 2: Now click on the create volume button at the top. Choose the space according to your requirements. We chose 2 GB of space, which means we are assigning 2 GB Lun to our EC2 Linux instance. Make sure you select the same AZ as the EC2 instance to avoid delays.

How to attach and mount EBS volume on ec2 Linux instance on AWS

Step 3. You will notice a newly created EBS volume with the status “available”.

How to attach and mount EBS volume on ec2 Linux instance on AWS

Step 4: Now the next step is to attach and install the EBS Volume on your EC2 Linux instance. To do this, select the newly created EBS volume with the right mouse button on it and select the “Attach volume” option. Select an instance and click the attach button.

How to attach and mount EBS volume on ec2 Linux instance on AWS

Step 4: Now log into your Linux EC2 instance to check and confirm the newly attached volume EBS with fdisk -l command or lsblk command as shown below:

[[email protected] ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
 
Disk /dev/xvda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
 
 
# Start End Size Type Name
1 2048 4095 1M BIOS boot parti
2 4096 20971486 10G Microsoft basic
 
Disk /dev/xvdf: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
 
[[email protected]~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 10G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 10G 0 part /
xvdf 202:80 0 2G 0 disk

You will notice that we have a new disk added xvdf with a capacity of 2G to a Linux server.

Step 5: Format EBS volume as ext4 file system using the following command.

	
[[email protected] ~]# mkfs.ext4 /dev/xvdf
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
 
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Step 6: Create one directory “/ EBS_volume” as mount point and mount it using mount command as shown below:

# mkdir /EBS_volume
# mount /dev/xvdf /EBS_volume
# df -h /EBS_volume
Filesystem Size Used Avail Use% Mounted on
/dev/xvdf 2.0G 6.0M 1.8G 1% /EBS_volume

To turn off volume, you have to use the following command.

# umount /EBS_volume

How do I mount it automatically on next reboot?

You can see that in step 6 the DF command shows that the new mountpoint is successfully installed, however, this mountpoint is not persistent across a reboot, which means that / EBS_volume will not be automatically mounted on the next reboot. To avoid this case, follow the next step.

Step 7: Add below entry to file and / etc / fstab as shown below:

/dev/xvdf       /EBS_volume   ext4    defaults,nofail        0

Step 8: Now install it with the following command

# mount -a

If you do not run into any errors after this command, then it means that everything is in order. Now, on every reboot, / EBS_volume will be installed automatically.

This is how we can attach and mount an EBS volume on an ec2 Linux instance on AWS.

Sidebar