Archive

Posts Tagged ‘swap’

How to add Swap to EC2 micro instances

2 February 2013 Leave a comment

About an year ago I decided to move a few Coldfusion legacy web applications to a cheaper infrastructure.

Amazon web services Elastic Compute Cloud ( EC2 ) looked like a good choice. It offers a one year “free tier”, that means you can run some basic services for FREE for one year! For example, you can create a “micro istance” and run Linux on it for 750 hours/month. Even without free tier it’s really cheap but… is it enough?

I was mainly interested in creating a server host running Apache + Railo + MySQL . So I did it… there are tons of “how-to” documents and tutorials about that on the net.

My apps ran fine for one year til this week, when my customers started to complain. Occasionally, Railo’s embedded tomcat crashed without an apparent good reason, according to the log files. I won’t tell you all details of my debugging day, however, this thread really helped me to find out the truth.

Actually I did suspect the problem was in memory… let’s face it: you’ve got only 600MB RAM… what can you do with it, seriously? The answer is: a lot! Linux is an awesome OS when it comes to resources usage and management. Despite of that, the Ram was always full (near 300MB for Railo, 200 MB for MySQL), so… first thing I did was moving MySQL. Where? AWS got the solution: it’s called RDS, and it’s another Amazon service for hosting a fully functional Relational DBMS. After that change, I gave more breath to the Railo application server by assigning more memory (heap+non heap) to its jvm.

This worked for a while but soon I was still having the same issue. (D’OH!)

Going ahead in searching Goooogle and reading that post took me to a scary question…. “do I have a swap partition ?” Every linux purist knows that running systems without it is as dangerous as opening hell’s gates.

But WHY? Why Aws guys don’t equip their ec2 instances with a default swap space??

Well, there’s an answer and it’s related to costs. EBS storage mechanism charges you for every bit you read and write to and from disk. Every I/O operation make you spend money, so if you have a very “swappy” instance, you will be charged significantly for it, so creating the swap partition should be a ‘conscious’ operation.

Now please shut up and look how that is done:

1. switch to root with sudo su - ( in ubuntu, e.g.)

2. type the following command ( count is the desired block size )
dd if=/dev/zero of=/swapfile bs=1M count=1024

3. Setup the swap file with the command:
mkswap /swapfile

4. Enable the swap file:
swapon /swapfile

5. Make this change persistent by adding the following entry into /etc/fstab:
/swapfile swap swap defaults 0 0