Mount EBS to EC2
Updated: Aug 5, 2022
Attach and mount an EBS volume to an EC2 instance
Objectives:
1. Learn to create an EBS Volume and attach.
2. Mount this instance to an existing EC2 instance.
Step 1: Create a Linux Server in subnet ap-south-1b.
You can choose the subnet in Step3: Configure Instance Details of the instance creation.
You can refer to our step wise blog here for launching EC2 Linux Server.
Confirm that the instance state is Running.
Step 2: Create an EBS volume and attach it to the EC2 instance.
From the Left Navigation Pane, open the dropdown for Elastic Block Store and click on Volumes.
Here, click on Create Volume for creating an EBS Volume.
Step 3: Fill the details as follows:
Make sure the Volume Type is General Purpose SSD (gp2)
Size (GiB): 20
Availability Zone: ap-south-1b (This zone should be the same as your EC2 instance).
Click on Add Tag and set:
Key: Name
Value: Data
Click on Create Volume.
Step 4: After creating the volume, go back to your EBS volume page and select the volume named Data.
Open the dropdown for Actions and click on Attach Volume.
Click on the textbox for Instance.
It will show all the EC2 Instances we have in that particular availability zone.
Select the instance in ap-south-1b created above.
Click on Attach and now your EBS Volume is attached to your EC2 instance.
Step 5: Go back to Instances.
Select your EC2 instance and click on Connect.
Confirm the User name ec2-user. Click on Connect.
Step 6: Once you’re in the SSH run the following command for getting any system updates.
sudo yum -y update
Run the command lsblk to get a list of all the block storage modules associated with the instance.
Now check if the volume has any data using the following command:
sudo file -s /dev/xvdf
If your volume is named something like dev/xvdb you might need to change the command with your respective storage name.
If the above command output shows /dev/xvdf: data it means your volume is empty.
Step 7: Format the volume to the “ext4” filesystem using the following command:
sudo mkfs -t ext4 /dev/xvdf
Again, Run the command sudo file -s /dev/xvdf to check if the file system has been mounted on the Block or not.
As you can see now we have the file system mounted on our EBS volume.
Step 8: Create a new directory named newvolume with the command
sudo mkdir newvolume
Now mount your EBS volume in this directory using the command
sudo mount /dev/xvdf newvolume/
Note: this command has no output text
Change directory into newvolume directory and check the disk space to validate the volume mount.
cd newvolume
This following command is used to display the disk space used in filesystem in a human readable format
df –h
As you can see here the Block Volume dev/xvdf is successfully mounted on your EC2 Instance
Step 9: To unmount the EBS volume, first we need to go back to the main working directory using following command:
cd ..
After that put the following command to unmount the volume:
sudo umount /dev/xvdf
Note: Close the session, terminate the instance and delete the elastic volume if you no longer need it.