Swap Files and AWS - Extending Your EC2 Free Tier Instance Memory

Chris Davis

Amazon Web Services, or AWS, is the biggest provider of cloud computing services in the world. Even services like Heroku are a wrapper for AWS instances that applications sent to Heroku are deployed on. For developers first starting out with AWS, or for experimental applications that are intended for proof of concept or tech demos, the most cost-efficent way to deploy an application on AWS is to use the free tier of services.

An EC2 instance is the core of a cloud application, it represents the virtual hardware the application is running on. This includes the CPU, RAM, and network adapters. An issue that can easily come up is running out of memory, as the free tiers of AWS limit your EC2 instances to only 1 GB of RAM. For setup scripts and other functions, this limit can easily be hit, causing an out of memory error and a hard crash. Unfortunately, there’s not a way to increase the memory of the EC2 instance while remiaining on the free tier, as AWS ties all the specs together for the most part. This means a bump to RAM also increases processing power and CPU count, which may be underutilized if it was not the bottleneck.

A clever way to sidestep this limitiation is by adding a swap file. For those unfamilar, a swap file, also called a page file, is a temporary file or partition on a hard drive where a computer can dump and read from if memory is fully utilized. This is obviously much slower then utilizing RAM as you lose a significant hardware advantage, however, this is the perfect solution for experimental apps that don’t have users yet since performance is less important then staying in the free tier of AWS.

There are a few ways to add a swap file to an EC2 instance. The simplist way is to add a swap file, as in an an actual file that is designated by the system as a swap file. The offical Amazon documentation reccomends doing so by using the following steps:

  1. Use the dd command to create a swap file on the root file system. In the command, bs is the block size and count is the number of blocks. The size of the swap file is the block size option multiplied by the count option in the dd command. Adjust these values to determine the desired swap file size.

    The block size you specify should be less than the available memory on the instance or you receive a “memory exhausted” error.

    In this example dd command, the swap file is 4 GB (128 MB x 32):

    $ sudo dd if=/dev/zero of=/swapfile bs=128M count=32

  2. Update the read and write permissions for the swap file:

    $ sudo chmod 600 /swapfile

  3. Set up a Linux swap area:

    $ sudo mkswap /swapfile

  4. Make the swap file available for immediate use by adding the swap file to swap space:

    $ sudo swapon /swapfile

  5. Verify that the procedure was successful:

    $ sudo swapon -s

  6. Enable the swap file at boot time by editing the /etc/fstab file.

    Open the file in the editor:

    $ sudo vim /etc/fstab

    Add the following new line at the end of the file, save the file, and then exit:

    /swapfile swap swap defaults 0 0

The other way this can be accomplished is using an entirely seperate Elastic Block Storage disk. This is helpful if your current EBS disk is so full you would not have the space to add a swap file. In this example, the new disk will be mounted as /dev/xvdd (It may change in your case)

  1. As above, we will use the mkswap command, but this time target the entire volume, like so:

    $ sudo mkswap -f /dev/xvdd

  2. Make that volume avalible as swap space:

    $ sudo swapon /dev/xvdd

  3. Verify that the procedure was successful:

    $ sudo swapon -s

  4. Enable the swap volume on boot by editing the /ect/fstab file.

    $ sudo vim /etc/fstab

    Add the following new line at the end of the file, save the file, and then exit:

/dev/xvdd swap swap defaults 0 0

Remember to replace the dev/xvdd to the name of your volume, if required!

This procedure will allow your free tier EC2 instances to be more flexible while remaining as low cost to operate as possible.