Enable swap file on Amazon EC2 instance

This was done on a t2.medium instance with 4GB of RAM and a 50GiB EBS volume. The primary reason was to give the webserver a bit of leeway during high traffic periods. Stops it running out of memory and falling over.

Most of this is taken from the Amazon docs.

# Create a 6GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1G count=6

#Update the permissions
sudo chmod 600 /swapfile

#Set up the swap
sudo mkswap /swapfile

#Make the swap available
sudo swapon /swapfile

#Enable swap at boot
sudo nano /etc/fstab

#Add this line at the bottom
/swapfile swap swap defaults 0 0

Leave a Reply

Your e-mail address will not be published. Required fields are marked *