Attach and mount a new EBS volume to Amazon Linux 2 EC2 Instance

AWS allows you to create new EBS volumes and you can attach it to EC2 instance for extra storage. However, to make it usable as storage inside the instance, you need to mount it to a specific folder.

First create a new EBS volume and attach to the EC2 Instance by providing the preferred size , type and device mountpoint.

Connect to the Amazon Linux EC2 instance using the ec2-user Read here about default username on the EC2 instances

Read here about setting up the password authentication for Linux

Identify and format the disk with ext4

Run the below command to list the disk you attached to your instance.

[ec2-user@ip-172-31-40-130 ~]$ lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk 
└─xvda1 202:1    0   8G  0 part /
xvdb    202:16   0  16G  0 disk 

Check if the volume has any data using the following command.

[ec2-user@ip-172-31-40-130 ~]$ sudo file -s /dev/xvdb
/dev/xvdb: data

If the response shows “/dev/xvdb: data”, it means your volume is empty.

Format the volume to ext4 filesystem

[ec2-user@ip-172-31-40-130 ~]$ sudo mkfs -t ext4 /dev/xvdb
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
1048576 inodes, 4194304 blocks
209715 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
128 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the Filesystem to the mountpoint

Create a new folder to mount the filesystem

[ec2-user@ip-172-31-40-130 ~]$ sudo mkdir /datadir

Mount the file system on the created directory

[ec2-user@ip-172-31-40-130 ~]$ sudo mount /dev/xvdb /datadir/

Confirm the Disk mounted

[ec2-user@ip-172-31-40-130 ~]$ cd /datadir/
[ec2-user@ip-172-31-40-130 datadir]$ df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvdb        16G   45M   15G   1% /datadir

Setup Auto mount on every start

Open /etc/fstab file and make an entry like below

/dev/xvdb /datadir ext4 defaults,nofail 0 0

/dev/xvdb                                     /datadir    ext4   defaults,nofail   0   0

And verify if there are any errors by running mount -a

[ec2-user@ip-172-31-40-130 datadir]$ sudo mount -a
[ec2-user@ip-172-31-40-130 datadir]$

If the above command shows no error, your FSTAB command is correct and you are good to go


Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.