I have previously written on the subject of adding a swap file to an Amazon EC2 instance. However, that article recommended and focused on adding the swap file to the instance storage (also known as ephemeral storage) that comes with certain Amazon EC2 instances.
The problem is that, in lots of use cases, your chosen Amazon EC2 instance type may not come with any instance storage – like the popular T2 instances which are EBS backed only.
This article discusses how you can still leverage the concept of swap files even on “EBS only” Amazon EC2 instances.
If you’re not familiar with swap files and virtual RAM at all, read my older post to get an idea of why we often need one: Swap File For Ubuntu On Amazon EC2 – Why And How?
What’s Cool About The Following Swap File Process?
One thing I really like about the process described below is that unlike the process in my previous article, the swap file we will add using the below process will persist even after the system is stopped and later started. This is a huge advantage over instance/ephemeral storage swap files which only manage to survive reboots but have to be recreated after a full stop-and-start machine cycle.
Of course, ephemeral storage has its own advantages as well (one of which is speed). But since the type of EC2 instances we’re working with here don’t have ephemeral storage at all, the speed advantage doesn’t apply.
In addition, unlike a few other swap file guides out there (that write some gigabytes of zeros to some portion of your root drive), my process below DOES NOT mess with your root drive at all. It uses its own small, dedicated EBS drive for swap.
Steps To Add A Swap File To An “EBS Only” Amazon EC2 Instance
Step 1: Decide on how much swap you will be adding. In this example, I will be adding a 3GB swap file to a t2.medium Amazon EC2 instance. For Ubuntu users, read here if you need help deciding on how much swap you should allocate.
Step 2: Run lsblk on your instance. Note the attached volume(s).
Step 3: Shut down the instance.
Step 4: Create a new EBS volume that you will use as your swap file (in my case, this would be a 3GB volume).
IMPORTANT: Make sure the volume is GP2 (SSD) to avoid paying extra for I/O.
Mount the new volume to /dev/sdf (this becomes /dev/xvdf).
Step 5: Start the instance.
Step 6: Run lsblk again on the instance to verify that the new volume is attached.
Step 7: Set up the swap area with this command:
sudo mkswap /dev/xvdf
Step 8: Turn on the swap with this command:
sudo swapon /dev/xvdf
Step 9: To make sure the swap persists across reboots as well as full stop-and-start cycles…
Backup the fstab file with this command:
sudo cp -iv /etc/fstab{,-$(date '+%y%m%d%H%M%S').bak}
Now add this line to the fstab file:
/dev/xvdf swap swap sw 0 0
Step 10: To confirm that the new swap file is in use, run the “swapon -s” command. To check the amount of swap space, run either the “top” or the “free” commands.
Step 11: To view the current (default) values to swapiness and cache pressure, run the following commands:
cat /proc/sys/vm/swappiness cat /proc/sys/vm/vfs_cache_pressure
Step 12: Stop and then start the instance and use any of the commands from Step #10 to verify that swap is still on.
Ever had to add a swap file to an “EBS only” Amazon EC2 instance? Did this article help you do that? Please share your comments and contributions below.
Leave a Reply