Back

How to Enable Swapfile on Ubuntu AWS Instances

AWS EC2 instances do not come preconfigured with a swapfile. This means that if your application runs out of free memory something’s probably gonna crash.

NOTE: You will need sudo access to do this!

This creates the swapfile in ephemeral storage. This is ideal for a number of reasons, but comes with one major caveat: While you can RESTART the instance with impunity, if you STOP the instance the ephemeral storage is lost. This means that if you need to STOP the instance for any reason, you must manually recreate the swapfile when you START it again.

You COULD put the swapfile on one of the EBS partitions, but that has two main problems. First, EBS is network-attached storage and as such is (relatively) slow. Not ideal for swap. Secondly, EBS partitions are billed (partially) based on iops, so if you have an application that’s going to hit swap a LOT, you could easily incur non-trivial iops charges.

 

Determine if your instance already has a swapfile

sudo swapon -s
Filename Type Size Used Priority

If you get actual output then you already have a swapfile:

Filename Type Size Used Priority
/mnt/swapfile file 8388604 0 -1

You can also run top to check the size and availability of your swap:

top - 15:58:14 up 123 days, 10:13, 2 users, load average: 0.01, 0.13, 0.21
Tasks: 79 total, 1 running, 78 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.0%us, 2.3%sy, 0.0%ni, 92.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3840468k total, 3684892k used, 155576k free, 71016k buffers
Swap: 8388604k total, 0k used, 8388604k free, 1885980k cached
...

The important bit is that last line. If you have no swap configured it will show up like:

Swap: 0k total, 0k used, 0k free, 1885980k cached


 

Create a swapfile

Having determined your lack of swapfile, do the following to create one.

Determine the size of swapfile you should (theoretically) need

Conventional wisdom would say to use (physical_memory * 2) but as long as you make is several GB you should be good.

Shove the swapfile into the ephemeral storage partition

You want to do this on AWS instances.

Make sure your instance has the ephemeral store mounted

(It should be by default.)

df -h
 


Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 60G 5.7G 51G 11% /
udev 1.9G 12K 1.9G 1% /dev
tmpfs 751M 184K 750M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 0 1.9G 0% /run/shm
/dev/xvdb 394G 8.3G 366G 3% /mnt

As long as you see a several hundred GB volume mounted to /mnt, you should be good to go. If you don’t see it, try:

sudo mount /dev/xvda2 /mnt

Make the actual swapfile

sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096

This will create a ~4GB swapfile in the root of the /mnt directory. The “if=/dev/zero” bit prefills the swapfile with zeros to preallocate the space. If you need more or less swap-space, set count to (number_of_gb * 1024).

Assign the swapfile and enable it

sudo chown root:root /mnt/swapfile
sudo chmod 600 /mnt/swapfile
sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile

Add the swapfile to fstab

BE SUPER REALLY CAREFUL WITH THIS. If you screw up your fstab, you could brick the instance.

sudo vim /etc/fstab



#add the following line to the end of the fstab:
/mnt/swapfile swap swap defaults 0 0
 


#should end up looking something like:
LABEL=cloudimg-rootfs / ext4 defaults 0 0
/dev/xvdb /mnt auto defaults,nobootwait,comment=cloudconfig 0 2
/mnt/swapfile swap swap defaults 0 0

Turn the swapfile on

sudo swapon -a


Double check that the swapfile is being used

sudo swapon -s
Filename Type Size Used Priority
/mnt/swapfile file 8388604 0 -1



top
top - 16:30:53 up 123 days, 10:45, 2 users, load average: 0.00, 0.02, 0.11
Tasks: 79 total, 1 running, 78 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.7%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3840468k total, 3694308k used, 146160k free, 72632k buffers
Swap: 8388604k total, 0k used, 8388604k free, 1887192k cached
...

Congratulations!

You’ve set up and installed a swapfile on your EC2 instance and set it to be autoloaded on server restart.

Riley Mills
Riley Mills