Server Tips

How to get 4GB of swap on your EC2 micro instance at no additional costs!

So, you have your shiny new Amazon EC2 micro cloud server, but you need more than 1GB of RAM. There is a simple way to get much more performance out of your free instance by converting 4GB of your 8GB storage into a high performance swap drive. It is important to create the swap on the Instance Storage (ephemeral) disk and not an EBS device. Swapping will cause a lot of IO and will increase cost on EBS. EBS is also slower than the Instance Store and the Instance Store comes free with certain types of EC2 Instances.

Check to see if it is mounted to /mnt, if not just run

sudo mount /dev/xvda1 /mnt

Now, create a 4GB swap file. This will take about 20 seconds, so be patient.

sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096

Now, restrict your swap file

sudo chown root:root /mnt/swapfile
sudo chmod 600 /mnt/swapfile

Make and Flag it as a swap file

sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile

Add/Make sure the following are in your /etc/fstab

sudo nano /etc/fstab

add these lines to your /etc/fstab and save

/dev/xvda2 /mnt auto defaults,nobootwait,comment=cloudconfig 0 2
/mnt/swapfile swap swap defaults 0 0

Last step, enable swap and enjoy your performance boost!

sudo swapon -a

Leave a Reply