How to increase the storage of a Linux EC2 instance
Posted on
Before we get into the how to increase system storage on an EC2 instance, let me provide you with this cool terminal command to find out how much memory is available in a Linux instance.
df -h
The output should give you the size of the disk, how much is used and how much you have available
If it is your first time connecting to the instance you may be prompted to confirm the connection, answer yes
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Run the following command
lsblk
This should give you a list of volumes in your instance. In this example notice the last two entries. In my example the instance was originally 50Gb, and I increased it to 60Gb. The last two rows, the xvda has a size of 60Gb, but the only partition has a size of 50Gb. In order for our instance to use the newly allocated space, we need to increase the only partition to 60Gb.
ubuntu@ip-172-31-0-18:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 33.3M 1 loop /snap/amazon-ssm-agent/3552
loop1 7:1 0 32.3M 1 loop /snap/snapd/12883
loop2 7:2 0 55.5M 1 loop /snap/core18/2074
loop3 7:3 0 55.4M 1 loop /snap/core18/2128
loop4 7:4 0 67.6M 1 loop /snap/lxd/20326
loop5 7:5 0 25M 1 loop /snap/amazon-ssm-agent/4046
loop6 7:6 0 32.3M 1 loop
loop7 7:7 0 32.3M 1 loop /snap/snapd/12704
loop8 7:8 0 70.3M 1 loop /snap/lxd/21029
xvda 202:0 0 60G 0 disk <------------ Size of the disk
└─xvda1 202:1 0 50G 0 part / <---------- Needs to be increased to 60 for our instance to be able to use the newly allocated space
Since we are trying to increase the partition xvda1 on 202:1, the number after the 202 in my example, which is a 1 is the partition we are trying to increase in disk xvda. So the command looks like the following: “sudo growpart DISK_ADDRESS PARTITION_#”. So in our example it looks like the following.
sudo growpart /dev/xvda 1
Running the above command should yield a result like so.
If you run the “lsblk” command we run in the previous step you are going to see that the partition has been resized to the total value of the disk:
ubuntu@ip-172-31-0-18:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 33.3M 1 loop /snap/amazon-ssm-agent/3552
loop1 7:1 0 32.3M 1 loop /snap/snapd/12883
loop2 7:2 0 55.5M 1 loop /snap/core18/2074
loop3 7:3 0 55.4M 1 loop /snap/core18/2128
loop4 7:4 0 67.6M 1 loop /snap/lxd/20326
loop5 7:5 0 25M 1 loop /snap/amazon-ssm-agent/4046
loop6 7:6 0 32.3M 1 loop
loop7 7:7 0 32.3M 1 loop /snap/snapd/12704
loop8 7:8 0 70.3M 1 loop /snap/lxd/21029
xvda 202:0 0 60G 0 disk <---------------- Size of the disk
└─xvda1 202:1 0 60G 0 part / <--------------- Was 50 now it is 60 matching the size of the disk
The last step is to increase the file system itself. You can do so with the following command: “sudo resize2fs PARTITION_ADDRESS”
sudo resize2fs /dev/xvda1
Should return an output like so:
ubuntu@ip-172-31-0-18:~$ sudo resize2fs /dev/xvda1
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 8
The filesystem on /dev/xvda1 is now 15728379 (4k) blocks long
If you want to check that everything worked correctly and your available system storage through the terminal run the following command:
df -h
The output should give you the size of the disk, how much is used and how much you have available
You are right! I changed the instances where it said memory and changed it for storage. Sorry about the confusion. I believe to change the RAM memory you need to change the instance itself, and there is no way around that in AWS.
The article title says it will demonstrate how to increase the memory but you are increasing the storage. Memory and storage are not the same.
You are right! I changed the instances where it said memory and changed it for storage. Sorry about the confusion. I believe to change the RAM memory you need to change the instance itself, and there is no way around that in AWS.