Analogue's Blog

home

Adding swap to an EC2 micro instance on Amazon

24 Apr 2011

Adding swap to an EC2 micro instance on Amazon

Amazon EC2 provides cheap micro instances with a small amount of memory and no swap. If you need swap on your instance you must add it manually.

See for yourself, no swap:

$ free
total       used       free     shared    buffers     cached
Mem:        611212     119544     491668          0      42460      36636
-/+ buffers/cache:      40448     570764
Swap:            0          0          0

Here’s how to add a 1GB swap to your running micro instance:

$ sudo dd if=/dev/zero of=/var/swapfile bs=1M count=1024
$ sudo mkswap /var/swapfile
$ sudo swapon /var/swapfile

And now you can check that you have 1GB of swap:

$ free
total       used       free     shared    buffers     cached
Mem:        611212     604092       7120          0      36692     514256
-/+ buffers/cache:      53144     558068
Swap:      1048572          0    1048572

The swap file won’t be used on next reboot, but it’s there as long as the instance is running. If you want to permanently add the swap file to your micro instance, add this line at the end of the /etc/fstab file:

/var/swapfile none swap sw 0 0

Now you have a micro EC2 instance with a 1GB permanent swap file on your root EBS filesystem.

Comments

RichBos

This is superb, thank you. EXACTLY what I was looking for, I have an AWS Kloxo build which was starting to complain…!

Richard.