Skip to content
TrySitePrice

How to add swap to AWS EC2/Lightsail Amazon Linux instance

Best/Tutorial

To add swap to an Amazon Linux EC2 or Lightsail instance on AWS, you can use the following steps:

  1. Connect to your instance via SSH.
  2. Check if there is any existing swap partition by running the command sudo swapon --show
  3. If there is no existing swap partition, create a new one by running the command sudo fallocate -l <size> /swapfile where <size> is the size of the swap partition you want to create. For example, to create a 4GB swap partition, you would run sudo fallocate -l 4G /swapfile
  4. Change the permissions of the swap file to 600: sudo chmod 600 /swapfile
  5. Mark the file as swap space: sudo mkswap /swapfile
  6. Enable the swap file: sudo swapon /swapfile
  7. Make the swap partition persistent across reboots, by adding the following line to /etc/fstab /swapfile swap swap defaults 0 0
  8. You can check the swap status by running free -h
rnPlease note that you should use the appropriate size for your specific use case, and also that swap usage can impact your performance, so monitor the usage and adjust accordingly.