On multiple occasions we had to deal with instances running out of disk space for the root file system. AWS provides you reasonable amount of storage, but most operating systems without additional settings will just use the root partition for everything. Which is usually sub-optimal, since default root partition is 8GB and you may have 160GB SSD just mounted on /mnt and never used. With the AWS Web interface, it is easy. Just create bigger root partitions for the instances. However, the AWS CLI solution is not obvious and somewhat hard to find. If you need to regularly start instances with non-standard root partitions, manual approach is not maintainable.
There is a solution. It lies in the –block-device-mappings parameter that can be passed to aws ec2 run-instances command.
According to the documentation this parameter uses JSON-encoded block device mapping to adjust different parameter of the instances that are being started. There is a simple example that shows how to attach additional volume:
1 |
--block-device-mappings "[{\"DeviceName\": \"/dev/sdh\",\"Ebs\":{\"VolumeSize\":100}}]" |
This will attach additional 100GB EBS volume as /dev/sdb. The key part: “Ebs”: {“VolumeSize”: 100}
By specifying your instance’s root partition you can adjust the root partition size. Following is an example how to create Amazon Linux instance running on t1.micro with 32GB root partition:
1 |
aws ec2 run-instances --image-id ami-fb8e9292 --count 1 --instance-type t1.micro --key-name test-key --security-groups test-sg --block-device-mapping "[ { \"DeviceName\": \"/dev/sda1\", \"Ebs\": { \"VolumeSize\": 32 } } ]" |
The resulting volume details show the requested size and the fact that this is indeed root partition:
Confirming, that the instance is operating on the proper volume:
1 2 3 4 5 6 |
:~> ssh ec2-user@ec2-50-16-57-145.compute-1.amazonaws.com "df -h" Filesystem Size Used Avail Use% Mounted on /dev/xvda1 32G 1.1G 31G 4% / devtmpfs 282M 12K 282M 1% /dev tmpfs 297M 0 297M 0% /dev/shm :~> |
There is enough space in the root partition now. Note: This is EBS volume, additional charges will apply!
References
- aws ec2 run-instances help
Related Posts
- Small Tip: How to use AWS CLI ‘–filter’ parameter
- Small Tip: How to use –block-device-mappings to manage instance volumes with AWS CLI
- Small Tip: How to use AWS CLI to start Spot instances with UserData
- Small Tip: EBS volume allocation time is linear to the size and unrelated to the instance type
- Small Tip: Partitioning disk drives from within UserData script
Thanks! Great tip.