Add EBS based swap to an EC2 instance

Add EBS Swap To AWS EC2 Instance
1. Create a new EBS volume of around 4-8GB and attach it to your instance.
We will assume the volume is /dev/xvdb
To check wether it is mounted use lsblk

2. If your volume is mounted, unmount it

sudo umount /dev/xvdb

3. Turn this volume into swap

sudo mkswap /dev/xvdb

4. Enable this volume as swap

sudo swapon /dev/xvdb

5. Make sure it is working as swap

swapon -s

6. Use this disk as swap if instance is restarted

sudo vim /etc/fstab

Add this:

/dev/xvdb       none    swap    sw  0       0