I needed swap for a t2.micro ec2 machine because it was dying almost twice a week, so I created a swap file for this.
Run as root or the following commands:
fallocate -l 1G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile
let’s add the following line to the /etc/fstab file
/swapfile swap swap defaults 0 0