Increase EBS volume size attached to EC2
Tags: aws, ec2, ebs, beginners
Published: 2023-07-23 06:14:34

This page provides a guide on increasing AWS EC2 root EBS volume size when EC2 is running.
When launching an EC2 instance on AWS console, an 8 GiB EBS volume will be attached as default, aka root volume. Suppose, your EC2 instance is running for a long time, and the root volume is full. You want to expand the volume size without stopping running instance. You have two options:
1. Expand the root EBS volume size
2. Attach a new EBS volume to EC2 instance

There are some concepts you need to keep in mind before we go through the details.
- Data volume availability – To make the data volume available, you need to format and mount the data volume.
- File system – To take advantage of the increased size of the data volume, the file system needs to be extended.

## Expand the root EBS volume size

![expand-root-volumn-size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxnqfsvpafufcpqkppmc.png)
Here is a screenshot before modification. The EC2 instance is running with an 8 GiB root EBS volume attached, which is the default size of attached EBS volume when launching an EC2 instance from AWS console.

![storage-original-8gb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bi1v4prba4487n1y5u0i.png)
SSH login to instance, and run **df -hT** to show the information about the file system. The Filesystem **/dev/xvda1 ** is what we need to expand. Currently it's 8.0G and has 1.6G used.

![df-ht-8gb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4veqcmjt8mmdapyzd9jc.png)
Use the following process when expanding a volume root size:
1. (Optional) Before modifying a volume that contains valuable data, it is a best practice to create a snapshot of the volume in case you need to roll back your changes. For more information, see Create [Amazon EBS snapshots](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html).
2. Request a volume modification. 
From AWS console, select the volume to modify and choose Actions, Modify volume. The Modify volume screen displays the volume ID and the volume's current configuration, including type, size, IOPS, and throughput. Enter a new value, for example 20 for Size. After you have finished changing the volume settings, choose **Modify**. The volume size changes to 20 as follows.
![storage-expand-20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llp1hpbu08xj7dbq6tko.png)

3. Extend a Linux file system after resizing a volume
After you increase the size of an EBS volume, you must use the file system–specific commands to extend the file system to the new, larger size. You can do this as soon as the volume enters the optimizing state.
To extend a file system on Linux, you need to:

- Extend the partition, if your volume has one
- Extend the file system

Use **lsblk** command to view your available disk devices and their mount points to help you determine the correct device name to use. The root device is **/dev/xvda**, which has three partitions named **xvda1**, **xvda127** and **xvda128**.
![lsblk-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fjxd874tun12m5i3pce.png)
Use the **lsblk -f** command to get information about all of the devices attached to the instance. The **FSTYPE** column shows the file system type for each device. Device partition **xvda1** is formatted using **XFS** file system, while partition **xvda128** is **VFAT** file system. If the column is empty for a specific device, it means that the device does not have a file system. 
![lsblk-f](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1ausrhxkj5ynh9c6fld.png)
We choose partition **xvda1** to extend. As the partition file system type is XFS, use the **growpart** command to extend. Note the space between the device name (xvda) and the partition number (1) for partition **xvda1**.
![growpart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sct61p7bwbkoxevzehde.png)
Run **lsblk -f** command to verify the partition **xvda1** size has been extended to 20 GiB.
![lsblk-f-20gb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rttik8hbp0ztro9460oh.png)
Now, extend the file system. Get the name, size, type, and mount point for the file system that you need to extend. Use the **df -hT** command.
![df-ht-20gb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul1btrtdqlet9lrfrrh9.png)
The commands to extend the file system differ depending on the file system type. Choose the following correct command based on the file system type that you noted in the previous step. For the **XFS** file system, use the **xfs_growfs** command and specify the mount point of the file system that you noted in the previous step.
![xfs_growfs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kto44uj5pvsethw52ff6.png)
Verify that the file system has been extended using the **df -hT** command.
![df-ht-verify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjwjuieids1782hydemd.png)

## Attach a new EBS volume to EC2 instance

![attache-new-volume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k958dz8cd2ej4faecmcz.png)
AWS EC2 instance is allowed to attach multiple EBS volumes. An alternative to increasing the EBS volumes is to attach additional EBS volumes. 

1. Create an EBS volume with 100GiB on AWS console
From AWS console, go to **Volume** tab and click **Create Volume**. The Create Volume screen shows the volume's default configuration, including type, size, IOPS, and throughput. You can keep these fields as default, and create a volume with 100 GiB available. 
![create-volume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3oqsvjr1cqow7o935m4.png)

2. Attach the newly created volume to EC2 instance
Select the volume to attach and choose **Actions**, **Attach volume**. Select the instance attached to, and give it a device name, for example **/dev/sdf**. See [Device names on Linux instances - Amazon Elastic Compute Cloud](https://docs.amazonaws.cn/en_us/AWSEC2/latest/UserGuide/device_naming.html?icmpid=docs_ec2_console#available-ec2-device-names) for more information about device name recommendations from AWS.
The new volume state is in-use after attached.
![volumes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ku7cwprlftfov6vyjf53.png)

3. Make the data volume available for use
Now we have a new volume with 100 GiB attached to instance. Use the following procedure to make the data volume available for use. 
Firstly, SSH into the instance, format and mount the data volume.
In the terminal window, use the following command to view your available disk devices and their mount points. The output of **lsblk** removes the **/dev/** prefix from full device paths. The output shows that there are two devices attached to the instance - the root device **/dev/xvda** and the newly attached volume **/dev/xvdf**. In the demo, you can see the column **FSTYPE** is empty for the new volume **/dev/xvdf** which means the new volume does not have a file system.
![new-volume-df-ht](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b836cmehdr9gfrmu9luk.png)
To create a file system, use the following command with the data volume named **/dev/xvdf**. As the root volume has an **XFS** file system, you should format the new volume with **XFS** as well. 
For another file system, find the details from [Extend a Linux file system after resizing a volume - Amazon Elastic Compute Cloud](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html).
![mkdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztdja5w1aju2ect4lpri.png)
To create a mount point directory for the new volume, use the **sudo mkdir /data** command. The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume. The following example creates a directory named **/data**. And mount the data volume in the directory we created, use the following mount command with the new volume name (**/dev/xvdf**) and the mount point directory name (**/data**). Verify that you have formatted and mounted your data volume. 
The following example output shows that the data volume **/dev/xvdf** has an XFS file system and is mounted at the **/data** mount point.
![mkdir](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4gfkhp0kjtbcwvogmbj.png)
Now, we have an additional EBS volume attached to EC2 successfully. The instance can make use of 100 GiB volume. 

## Summary
1. Newly created EBS volume is a block volume, and must have file system initialized after attached to instance.
2. To make the data volume available, you need to format and mount the data volume. Format means creating a file system on volume.
3. To take advantage of the increased size of the data volume, the file system needs to be extended.
4. Use command **sudo mkfs -t xfs /dev/xdaf** to create a file system. **xfs** is the type of file system.
5. Use command **sudo mount /dev/nvme1n1 /data** to mount data volume to a mount point directory.
6. The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume.
7. Useful command:
  - **sudo lsblk -f** : shows the devices, file system type and mount point attached to the instance
  - **df -hT** : shows the file system information
  - **sudo xfs_growfs -d /data** : extend the XFS file system that is mounted on the **/data** mount point.

Thanks for reading and appreciate your comments on content and grammar!