How to increase the size of an Amazon EC2 EBS Instance (with no downtime)
I’m often asked by people who read my posts on AWS, how they can increase the hard drive size of their EBS instance. i.e. how do they change the size of their / partition.
Luckily, for most systems with up-to-date software running on them, this is actually quite easy to achieve.
The following method worked for me but please be careful when doing this on your own system as it could cause irretrievable loss of data.
As a result create a full backup before attempting this.
The following assumes you are only running one instance that you want to upgrade, but the principles apply whatever. It was carried out on an Amazon Linux 64 bit image (originally 8GB, resized to 250GB) which had all software updated via yum update before starting.
Take a backup and create a load balanced system – if you’re not already using one
- Create an AMI of your current running instance, this acts as a perfect backup of your current system and allows to deploy a copy to test this process with.
- Launch a new instance from the AMI you just made
- Create an ELB with this new instance behind it and update your app, DNS whatever to filter traffic through the ELB
- When you’re sure your new instance is taking the traffic for your app add the existing instance to the load balancer, so you’re now load balancing across both instances. Having two instances to work with allows us to avoid down time as instance two can take the traffic while we work on instance one.
Resize the hard disk
- Stop the instance: AWS Management Console -> EC2 -> Instances, Right click instance name and choose “Stop”
- Note down the name of the volume attached to the instance: AWS Management Console -> EC2 -> Instances, click the instance name and click the link next to “Block Devices” in the panel (e.g. labelled sda1) which gives you the details of the selected instance. Note down the “EBS ID” value (e.g. vol-12a3599a).
- Create a snapshot of the current HDD: AWS Management Console -> EC2 -> Elastic Block Store -> Volumes, search for the EBS ID you noted down, right click it and choose create snapshot. Also note down the device name from the Attachment information column, this will be something like /dev/sda1
- Switch to the snapshots list (AWS Management Console -> EC2 -> Elastic Block Store -> Snapshots) and look for the one with a status of pending – this is your new snapshot being created. Note down the Snapshot ID and wait for the operation to complete.
- Go back to the volumes list: AWS Management Console -> EC2 -> Elastic Block Store -> Volumes, and hit create volume. Set the disk size you want and select the snapshot from the list with the ID you noted down.
- Wait for the volume to be created (you should be able to sort the volume list by status and see one which is available to find it if you have a lot of volumes).
- Disassociate the existing volume from your instance: Right click the volume you created the snapshot from and choose disassociate.
- Attach the new bigger volume to your instance: Right click the available volume you created and choose associate, select the instance you want to attach it to from the list and enter the device name as that which you noted down earlier (e.g. /dev/sda1). Note though this may be the original device name, when started back up this may be renamed to something /dev/xvda1 on the instance itself due to the way modern linux kernels reference the devices.
- Start your instance: AWS Management Console -> EC2 -> Instances, Right click instance name and choose “Start”
- ssh into your instance and confirm that the instance now has more space available. To do this run fdisk -l /dev/xvda1 (replacing /dev/xvda1 with your device name). You should get something back like this:Disk /dev/xvda1: 268.4 GB, 268435456000 bytes
255 heads, 63 sectors/track, 32635 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
- However if you run something like df -H you’ll see that the partition still has the size originally allocated to it, so we have to expand the partition to cover the additional space
Expand Your Partition
- Check your instance file system type by running: fsck -N /dev/xvda1
- If it is one of ext2, ext3, or ext4 you can use resize2fs to expand the filesystem into the available space. You can do this with the instance running if your file system is ext3 or ext4 and you’re using kernel version 2.6+ by simply executing something like this (this example resizes to 240Gb):resize2fs /dev/xvda1 240G
- Other filesystem types have different on-line (running instance) resizing capabilities and you should check the man pages for your filesystem to check which you should use.
- After a short amount of time your instance should be able to access the extra space. At this point I generally like to reboot my instance to ensure clean running but this is probably unnecessary.
- Add the instance back into your load balancer if it’s not there already and when happy remove or update the other instance and remove the ELB if required.