Swap space in Linux can be used when a system requires more memory than it has been physically allocated (RAM). When swap space is enabled, Linux systems can swap infrequently used memory pages from physical memory to swap space (either a dedicated partition or a swap file in an existing file system) and free up that space for memory pages that require high-speed access. 
Follow steps to create EBS Volume in AWS Console, that we'll use as a swap memory.
- Go to EC2 Dashboard
- Select EBS Volumes
- Create Volume
- Recommended size of swap volume is 2 times of RAM 
- Select availability zone same as your EC2 instance
Select EBS Volume that we just created for swap and your EC2 instance.
lsblk to check new volume, it will appear as
/dev/xvdf on instance.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 256G 0 disk └─xvda1 202:1 0 256G 0 part / xvdf 202:80 0 32G 0 disk
Set up the swap area
$ sudo mkswap /dev/xvdf
$ sudo swapon /dev/xvdf
Make this swap setting persist by adding following line in
$ sudo nano /etc/fstab /dev/xvdf none swap sw 0 0
$ sudo swapon --show NAME TYPE SIZE USED PRIO /dev/xvdf partition 32G 279.9M -1