You can create swap space using the following steps.
Here we are creating swap at /root/
dd if=/dev/zero of=/root/myswapfile2 bs=1024 count=4096
Here count is kilobyte count of swap space
mkswap /home/swapfile1
vi /etc/fstab
make entry
/root/myswapfile swap swap defaults 0 0
run
swapon -a
https://stackoverflow.com/questions/17173972/how-do-you-add-swap-to-an-ec2-instance