Create A Swap File In Your EC2 Instance Storage Space
If you are running Linux on Amazon EC2 with an EBS volume, you may have noticed your instance doesn't come with swap space. This is, apparently, by design to avoid unpredicatable costs with I/O requests. A runaway process may eat up all your memory and start swapping large amount of data to the swap partition/file, therefore, increasing the number of I/O requests. While I/O requests are pretty cheap at $0.10 per million requests, that cost could escalate if a process goes rogue and you don't notice it for a while. However, not having some swap space could mean a service running out of memory and crashing which, obviously, is not ideal either. So, what to do?
The best solution is to use stable software and configure it properly to maximize your available RAM without going over since swapping is slow. But that, of course, is unrealistic. Even the best sys admin misconfigures or fall victim to a poorly written piece of code.
A more realistic approach would be to create swap space in some storage area that is not going to incur a cost if swapping does occur. If you are using an EBS volume, that storage area already exists and is probably just sitting there unused.
All EC2 instances come with some instance storage (i.e 160GB on a m1.small). But, for many good reasons that are beyond the scope of this article, an EBS volume is used instead. So, the instance storage just lie dormant, unused, but still available to you.
This instance storage is a perfect place to hold your swap space because:
- It makes use of storage which you are already paying for.
- I/O requests on instance storage does not incur a cost. Only EBS volumes have I/O request charges.
- Swap data is temporary. So, it doesn't matter if data in your instance storage is lost after a reboot.
With that in mind, let's create some swap space!
- Find out where your instance storage is located. You can issue the df command which will show you a list of mounted file systems. On Ubuntu systems with the EBS volume mounted as the root device, the instance storage is mounted at
root@publicweb:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 50G 2.8G 45G 6% / udev 819M 12K 819M 1% /dev tmpfs 331M 176K 331M 1% /run none 5.0M 0 5.0M 0% /run/lock none 827M 0 827M 0% /run/shm /dev/xvdb 147G 188M 140G 1% /mnt
- Create a 2 GB file in /mnt on your instance storage.
dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048
- Make sure only root have access to this file since sensitive data may be written to it
chown root:root /mnt/swapfile chmod 600 /mnt/swapfile
- Make the file into swap space
mkswap /mnt/swapfile swapon /mnt/swapfile
- Update /etc/fstab with the following to mount the swapfile automatically after a reboot:
/var/swapfile swap swap defaults 0 0
- Turn on the swap space
Swap is now enabled and you can verify this by issuing the
free command to see your memory usage.