Tag Archives: Linux

Adding Swap Space to an AWS EC2 Micro Instance

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:

  1. Create an EBS volume and dedicate it to swap space.
  2. 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:

mkswap /mnt/swap_512.swap

Finally, I activated the swap file with the swapon command:

swapon /mnt/swap_512.swap

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:

swapon /dev/xvdf

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.

The Fat Aussie Barstard Blue

Blue YouTube Videos!

I’m using the Linux distribution Fedora 16 on my primary home computer at the moment. I don’t particularly like the GNOME 3 interface, so I’ll probably change to Linux Mint with Cinnamon shortly, but I’m stuck with this rather stupidly designed mess in the mean time. In fact I was about to change today before I found a fix for a very weird problem, blue skinned people on YouTube videos!

That’s right, you heard it correctly, the people on YouTube videos were blue, just like this one of The Fat Aussie Barstard here.

The Fat Aussie Barstard Blue

The Fat Aussie Barstard Blue

At first I thought it was some kind of joke from The Fat Aussie Barstard, as he is a bit of a character on YouTube. However, I played another video on YouTube and sure enough that also had a person with blue skin!

When I was using my Macbook Air yesterday I received an update for the Adobe Flash Player, so I had also updated my Linux Fedora 16 PC to the latest version (Adobe Flash Player 11.2). This seemed to be the cause of the problem, as other videos played through VLC  or Media Player were fine.

After a bit of googling I found a solution that has worked for me. You need to turn off the “Enable hardware acceleration” option on the Adobe Flash Player Settings window.

To do this, right-click anywhere on the video and select “Settings” at the bottom of the menu. You should then see the following window appear on top of the video where you need to de-select the check box option.

Adobe Flash Settings

Adobe Flash Settings

Once you have done that just reload the page to get the video playing with normal skin tones!

The Fat Aussie Barstard Normal

The Fat Aussie Barstard Normal

I’m not sure who is to blame for this latest example of why Linux distributions are NOT ready for the desktop. With this problem all the other idiotic choices that have been made for GNOME 3 it seems that RedHat only wants people to use Linux on servers and tablets!

I tried to compare the CPU utilization of a 1080p YouTube video with the hardware acceleration on and off, but when I had hardware acceleration on the video playback went completely haywire!

At least there is a quick and easy way to get YouTube videos with a blue tint back to normal, but there should be no need to fix such a simple problem in the first place. Clearly there was not enough testing before this update was pushed out!

Renaming a File Starting with “-” or Dash on Linux

I accidentally created a file named “-C” on one of my Linux servers when using the tar command. I was hitting my head against the wall for a while trying to work out how to use the mv command to rename the file to something more meaningful.

The problem is that the command line thinks that the “-C” is an option for the mv command, so you’ll keep getting an error like this:

-bash-3.2# mv -C archive.tgz
mv: invalid option -- C
Try `mv --help' for more information.

Even if you try and use quotes or the escape character it will still process the characters after the dash as an option to the command.

The solution is to tell the command that you have finished passing arguments by using double dash (“–“). Alternatively you can also use the full path to the file or “./-C”

-bash-3.2# mv -- -C archive.tgz

Hopefully this will help someone else who is looking to rename a file starting with “-“.