How to Attach and Mount an EBS volume to EC2 Linux Instance

Attach and Mount an EBS volume to EC2 Instance

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

Mount an EBS Volume to EC2 Linux

This tutorial will teach you how to attach and mount an EBS volume to ec2 Linux instances.

Follow the steps given below carefully for the setup.

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

Create EBS volume to attach to ec2 instance

Note: Make sure the EBS volume and the instance are in the same zone.

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

Step 3: Select the ec2 instance from the instance text box as shown below.

attach ebs volume

Step 4: Now, login to your ec2 instance and list the available disks using the following command.

 lsblk

The above command will list the disk you attached to your instance.

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 the ext4 filesystem using the following command.

sudo mkfs -t ext4 /dev/xvdf

Alternatively, you can also use the xfs format. You have to use either ext4 or xfs.

 sudo mkfs -t xfs /dev/xvdf

Step 7: Create a directory of your choice to mount our new ext4 volume. I am using the name “newvolume“. You can name it something meaningful to you.

sudo mkdir /newvolume

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

sudo mount /dev/xvdf /newvolume/

Step 9: cd into newvolume directory and check the disk space to validate the volume mount.

cd /newvolume
df -h .

The above command should show the free space in the newvolume directory.

To unmount the volume, use the unmount command as shown below..

sudo umount /dev/xvdf

Automount EBS Volume on Reboot

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

Follow the steps given below to automount the EBS volume to the ec2 instance.

Step 1: Back up the /etc/fstab file.

sudo cp /etc/fstab /etc/fstab.bak

Step 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       /newvolume   ext4    defaults,nofail        0       0

Step 3: Execute the following command to check if the fstab file has any errors.

sudo mount -a

If the above command shows no error, it means your fstab entry is valid.

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

Add Multiple EBS Volumes To EC2 Instance

You can add more than one EBS volume to an ec2 instance as an additional disk. You can follow the same steps explained above and the only difference will be the disk name that you get from the lsblk command.

Here is an example output having two EBS volumes attached to an instance.

Multiple EBS volumes on ec2 instance

Mount one EBS volume to Multiple EC2 Instances

If you have any use case to mount an EBS volume to multiple ec2 instances, you can do it via EBS multi-attach functionality.

This option only serves specific use cases where multiple machines need to read/write to the same storage location concurrently.

The EBS multi-attach option is available only for Provisioned IOPS (PIOPS) EBS volume types.

Attaching EBS volume to multiple ec2 instances.

Note: EBS multi-attach does not support XFSEXT2EXT4, and NTFS file systems. It supports only cluster-aware file systems.

How Many EBS volumes can I attach to an Instance?

You can attach a maximum of 27 EBS volumes to an ec2 instance. Also, this count changes for a few instance types. Please check the Volume limits document to know more.

Also, limit the number of EBS maximum volumes attached to ec2 instances to have optimal performance. Also, plan your instance capacity based on the workload you are trying to run.

For example, databases require high IOPS for high read-write rates. IOPS depends on the disk size. Higher the size, the higher the IOPS.

It is recommended to take snapshots of EBS volumes for high data high availability and restore options. Check out the EBS snapshot automation article for strategies and restore options.

Conclusion

In this article, you have learned how to mount an EBS volume to the ec2 instance.

As a DevOps Engineer, it is very important to understand all the EBS attach options when working on real-time projects.

If you get any errors during the setup, please feel free to contact us in the comment section.

40 comments
  1. I have created a Ec2 Amazon linux 2 instance and have attached 4 extra EBS volumes ( gp2 =3gb, io1=4gb, and io2=4gb) and mounted it to a particular directory ( /apps/vol/ebs_1 etc) and I have installed postgres db ( source code installation ) on it.
    I have created number of sample tables in the database and made my ebs root of defaultI instance volume full to 100%. Now I am creating more table i get the below error-

    ** Traceback (most recent call last): File “task15.py”, line 12, in cur.execute(“INSERT INTO task15 (id , url ,name , city ,description) VALUES (“+str(j)+”, ‘debian.com’,’Paul’,’Europe’, ‘California’ )”); psycopg2.errors.DiskFull: could not extend relation base/16387/16619: No space left on device HINT: Check free disk space. **

    I want to know how can, I save or move the 4 extra attaches EBS volumes in the database, so that even if my root storage is 100% the table i am creating with sample records or any files should be stored in any of the 4 volumes or any specific volumes automatically, so that I can analyze and delete the reset unused volumes.

    1. Hi Suraj,

      In the Postgres database configuration, you need to map the default data directory (ie, /var/lib/postgresql/) to any of the attached EBS volumes. So that all data will be stored in the extra volume.

  2. I have an instance with 2 EBS volumes in which 1 is root volume.
    I need to attach these 2 volumes to a new EC2 instance
    How this can be achieved?Thanks in advance

    1. Hi Oliver,

      You can take a snapshot of the root volume and create a new disk out to it. And then you can attach both volumes using the volume attach commands as given in the tutorial.

  3. Thanks for the post. I know this is a silly question. But, where do we run this commands?(what software do we use?) I’m using Putty software to connect to my instance. Can I run these commands on Putty?

    1. Putty is good..Once you are logged in to the server through putty, you can run these commands from putty and it will get executed on the remote server. Hope it answers your query.

  4. I am trying to attach a volume created from a snapshot to a newly spun up Ubuntu instance and keep getting the following error over and over:

    Error attaching volume: The instance configuration for this AWS Marketplace product is not supported. Please see the AWS Marketplace site for more information about supported instance types, regions, and operating systems.

    1. hi Orn, It looks like the AMI you are using doesn’t support external volumes. Try and different AMI and see if it getting attched.

  5. Thanks for the post. But, I’m facing one error

    I couldn’t able to unmount the volument, I’m facing a error as “””umount: /newvolume: umount failed: Operation not permitted”””

    Please sort this out ASAP

    1. I don’t know if you’ve already figured it out, but maybe you could try taking ownership of the volume:

      sudo chown `whoami` /newvolume

      Hope it helps

  6. not able to mount /dev/nvme2n1,getting error mount: /dev/nvme2n1 is write-protected, mounting read-only mount: unknown filesystem type ‘(null)’

  7. Hi! I did exactly that. But when I try to create files in the mounted directory, it says I have no permission. Any idea what I doing wrong?

  8. This is super helpful, thank you! Only one issue: once I got to the end, “sudo mount -a” gave me this error:
    -bash: $’sudo\302\240mount’: command not found
    However, “sudo mount” works without the -a option. Do you know why “sudo mount -a” has an issue with the non-breaking space (\302\240)? Thanks!

    1. You copied from the Web page. There’s a   in the markup. Just type the command in using your keyboard instead of using your clipboard.

Leave a Reply

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

You May Also Like