Monday, May 21, 2012

Amazon EC2 Micro Instance Swap Space - Linux

I have a Amazon EC2 Linux Micro instance. Since Micro instances have only 613MB of memory, MySQL crashed every now and then. After a long search about MySQL, Micro Instance and Memory Managment I found out there is no default SWAP space for Micro instance. So if you want to avoid the crash you may need to setup a swap space for your micro instance. Actually performance wise is better to enable swap.

Steps below show how to make a swap space for your Micro instance. I assume you have AWS Account with a Micro instance running.

  1. Run dd if=/dev/zero of=/swapfile bs=1M count=1024
  2. Run mkswap /swapfile
  3. Run swapon /swapfile
  4. Add this line /swapfile swap swap defaults 0 0 to /etc/fstab  
Step 4 is needed if you would like to automatically enable swap file after each reboot. 

Some useful command related to SWAP space:

18 comments:

  1. Thanks for the instructions. I haven't seen for sure whether it'll resolve my problem yet, but I guess I'll find out.

    Step 4 looks like it should have the first two items reversed, though? The second line of my fstab reads
    LABEL=/ / ext4 defaults,noatime 1 1

    and in the first column, everything else has a label like tmpfs, sysfs, and the next column paths like /sys, /proc, etc.

    ReplyDelete
  2. You may run 'sudo vim /etc/fstab' and add '/swapfile swap swap defaults 0 0' to the end f the file. I have added the snapshot of the fstab file to my post.

    ReplyDelete
  3. Interesting, it seems to work fine either way. Thanks for clarifying though.

    ReplyDelete
  4. Thanks! I was trying different approaches to solve some MySQL random crashes on an EC2 instance and this was it :)

    ReplyDelete
  5. Worked well until the step where I had to restart the server. Now it's not turning back on! Oy. Fridays are the worst.

    sudo shutdown now -r

    ReplyDelete
    Replies
    1. Why did you use shutdown? You didn't have to restart your instance.

      Delete
  6. Hi! I'm with same problem, but this steps don't resolved.

    ReplyDelete
  7. Cheers dude, this post at least got me able to start mysqld again. Let's see how it goes from here. :)

    ReplyDelete
  8. Great post, saved me a lot of time tearing my hair out. thanks.

    ReplyDelete
  9. Cheers! Is there a way to test if it's working or not?

    ReplyDelete
  10. Seems like this are valid for AMI linux, on ubuntu the first two columns on the last step are reversed. Doing it your way will prevent the instance from starting.

    ReplyDelete
    Replies
    1. Could you please post your /etc/fstab content for Ubuntu?

      Delete
  11. The etc/fstab config in here works fine on my micro ubuntu. Great post.

    ReplyDelete
  12. Thanks for the help, this really did work!

    ReplyDelete
  13. Leaving your swap file world-readable can lead to some serious security concerns should the image be comprimized. Bits of the memory will be readable by any user/service.

    To fix this issue you should also chmod 600 /swapfile

    ReplyDelete