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:

  1. It makes use of storage which you are already paying for.
  2. I/O requests on instance storage does not incur a cost. Only EBS volumes have I/O request charges.
  3. 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!

  1. 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 /mnt on device /dev/xvdb. For example:
    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
  2. Create a 2 GB file in /mnt on your instance storage.
    dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048
  3. 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
  4. Make the file into swap space
    mkswap /mnt/swapfile
    swapon /mnt/swapfile
  5. Update /etc/fstab with the following to mount the swapfile automatically after a reboot:
    /var/swapfile swap swap defaults 0 0
  6. Turn on the swap space
    swapon -a

Swap is now enabled and you can verify this by issuing the top or free command to see your memory usage.



This was super helpful, but there were some typos in your instructions in steps 4 and 5

Hi Quinton. Thanks for the comment. Can you please highlight the typo for me as I don't see it? Thanks. 

Swapfle should be swapfile at point 4 :) good tutorial BTW

Thanks and thanks :)

An impressive share! I have just forwarded this onto a coworker who has been conducting a little research on this.
And he actually bought me breakfast simply because I found it for him...
lol. So allow me to reword this.... Thank YOU for the meal!!
But yeah, thanks for spending the time to talk about this
matter here on your blog.

The fstab entry in step 5 should be
/mnt/swapfile swap swap defaults 0 0

instead of

/var/swapfile swap swap defaults 0 0


This is fine so long as you don't shutdown the instance. When you do that your ephemeral storage is removed and recreated when the instance starts again so then you have no swapfile.