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

See all Amazon AWS web services related articles/faq
So like many solo developers, I am tight on resources, especially money-wise. I have two AWS EC2 VMs running as WireGuard VPN on Amazon Linux for personal usage, and another one is my dev machine with Python, PHP and stuff. These are tiny VMs with just 512MB ram. The main problem is my little VM powered by CentOS or Amazon Linux 2 runs out of memory when I run “sudo yum update“. The BaseOS repo from RHEL and co is just too big. It requires a minimum of 3 GB ram. So I will also get a segfault when running out of memory as described here. I believe there is a dnf bug open in RHN. So I decided to take matters into my hands and add 8GB of swap space on Amazon Linux 2 EC2 / Lightsail VM running at AWS to fix issues.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category System Management
Prerequisites AWS account with EC2/Lightsail VM
OS compatibility AlmaLinux Fedora Linux RHEL Rocky Stream
Est. reading time 3 minutes

Procedure to swap to AWS EC2/Lightsail Amazon Linux instance

How to add swap to AWS EC2 - Lightsail Amazon Linux instance
  1. Login into your AWS EC2 / Lightsail Amazon Linux 2 VM using the ssh command:
    $ ssh ec2-user@ec2-nixcraft-wireguard-vpn
    $ ssh ec2-user@lightsail-ipv4-address-here
  2. Login as the root user using the sudo command:
    $ sudo -i
  3. Type the dd command to create a new 8GB swap file storage named /aws-swapfile-1 (1M * 8192 counts {8*1024} == 8GB):
    # dd if=/dev/zero of=/aws-swapfile-1 bs=1M count=8192 status=progress
    The status=progress option is passed to the dd command to display progress on your screen as follows:

    8588886016 bytes (8.6 GB) copied, 132.245227 s, 64.9 MB/s
    8192+0 records in
    8192+0 records out
    8589934592 bytes (8.6 GB) copied, 132.257 s, 64.9 MB/s
  4. The /dev/zero is used as a character stream for initializing data storage for the swap file.

  5. Set permission using the chmod command for security reasons:
    # chmod -v 0600 /aws-swapfile-1
    The verbose info indicates that only our root user and system Linux kernel can access the swap file:

    mode of ‘/aws-swapfile-1’ changed from 0644 (rw-r--r--) to 0600 (rw-------)
  6. Next use the mkswap command to set up a Linux swap area using the file named /aws-swapfile-1. For example:
    # mkswap /aws-swapfile-1

    Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
    no label, UUID=6713d1b4-13d0-44d5-908f-f7479da3401e
  7. Enable swap space on your AWS/Lighsail Amazon Linux for paging and swapping using the swapon command:
    # swapon -v /aws-swapfile-1
    The system confirming that I allocated memory to work as swap space in an Amazon EC2 instance:

    swapon: /aws-swapfile-1: found signature [pagesize=4096, signature=swap]
    swapon: /aws-swapfile-1: pagesize=4096, swapsize=8589934592, devsize=8589934592
    swapon /aws-swapfile-1
  8. Verify it using the swapon command/free command or top command/htop command:
    $ swapon -s
    $ htop
    $ free -h -t

  9. Edit the /etc/fstab file using a text editor and append the following line to turn on SWAP at boot time:
    /aws-swapfile-1 	swap 	swap 	defaults 	0 	0

And that is how you add swap space to AWS EC2/Lightsail Amazon Linux instance. Now my yum command worked without any memory issues:
$ sudo yum update

Summing up

I use swap space when running out of physical RAM on AWS EC2 or Lightsail VM. This page explained how to configure and use Amazon EC2 or Lightsail instances to swap space as a short-term substitute for physical RAM so that dnf command or yum command work without causing issues. I never had such problems with Debian, Ubuntu and other Linux distros, hence, I documented procedure to get around memory issues. So I hope Red Hat fixes this issue with yum/dnf. For more info read the docs using the --help option command or man command on your Amazon Linux or CentOS/RHEL VM:
$ man 5 fstab
$ man dd
$ man swapon
$ man mkswap

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • Ray Wang Aug 25, 2022 @ 10:12

    i think there is a typo, /etc/fstat should be /etc/fstab

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.