I recently moved some of the WordPress sites on an EC2 m1.small instance in Amazon Web Services (AWS) Singapore data centre to a m1.micro instance in the new Sydney data centre. Unfortunately the new t1.micro instance has run out of memory a couple of times, resulting in the websites being unavailable. After I was notified I had to find out what process/service was killed and restart it.
To stop this happening I could either upgrade to a m1.small instance with 1.7 GiB instead of the 613 MiB, or I could add swap space to the existing server.
I decided to try and save some money by adding swap space to my existing server. There are two main options to do this:
- Create an EBS volume and dedicate it to swap space.
- Create a swap file on the existing file system (also an EBS volume if you are using an EBS based AMI).
If you decide to add an EBS volume then the smallest size is 1 GB, for which you will have to pay the prevailing monthly rate in the AWS region of your instance. Make sure you put the EBS volume in the same availability zone as your instance or you will not be able to attach it! You will also be charged the I/O costs as data is written to and from the swap space.
With a swap file you only get charged the I/O costs, since you are already paying for the empty space. I still had quite a bit of space available and thought that 512 MB of swap space would be sufficient, so I chose this method.
I’ll describe what I did first and then outline the option using a dedicated EBS volume.
Adding a Swap File
First of I needed to decide where to put the swap file and what to call it. I chose /mnt/swap_512.swap and created it with the following command:
dd if=/dev/zero of=/mnt/swap_512.swap bs=1M count=512
Next I needed to let Linux prepare the file for use as a swap file by using the mkswap command:
Finally, I activated the swap file with the swapon command:
You can check to see if your swap file is being used with top or free. Here is the output from free on my machine:
total used free shared buffers cached Mem: 615664 592288 23376 0 8548 144448 -/+ buffers/cache: 439292 176372 Swap: 524284 348 523936
You should edit your /etc/fstab file to add the swap file information so that it is available if you restart the instance. This is what my fstab looks like on my t1.micro instance:
LABEL=/ / ext4 defaults,noatime 1 1 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sdf /u01 ext4 defaults 0 0 /mnt/swap_512.swap swap swap defaults 0 0
Note that I have another EBS volume attached for backup purposes that is mounted at /u01.
Adding a Dedicated EBS Volume for Swap Space
First you’ll need to create a new EBS volume in the same region and availability zone as your t1.micro instance. The smallest size is 1GB, which should be more than enough for your swap space.
Once the volume is available you need to attach it to your instance and then note the device so that you can refer to it later. For example, my backup volume is /dev/xvdf with a link to that called /dev/sdf.
There is no need to create the file like the previous example, so you can go straight to preparing the device for use as swap space with the following command:
mkswap -f /dev/xvdf
The -f will force the use of the whole disk, which is what you want.
You can then activate the swap space with:
Again you should edit your /etc/fstab to make sure the swap space is available upon a reboot. Just use my example above, replacing the /mnt/swap_512.swap with /dev/xvdf or your assigned device.
Adding swap space to your AWS EC2 micro instance is quite a simple process that can bring much needed stability (random process kills to keep the machine running are not fun). It can also give you time to catch a memory hogging process before it brings the machine to a halt. However, you do need to monitor your swap usage. If you find that your system is constantly reading and writing to swap then your system’s response time can slow down dramatically. In this case you are truly memory bound and need to upgrade to an m1.small instance.