Why don't EC2 ubuntu images have swap?

Solution 1:

You are right, the Ubuntu EC2 EBS images don't come with swap space configured (for 11.04 at least). The "regular" instance-type images do have a swap partition, albeit only 896 MB on the one I tested.

If some process blows up and you don't have swap space, your server could come to a crawling halt for a good while before the OOM killer kicks in, whereas with swap, it merely gets slow. For that reason, I always like to have swap space around, even with enough RAM. Here's your options:

  • Create an EBS volume (2-4 times the size of your RAM), attach it to your instance (I like calling it /dev/xvdm for "memory"), sudo mkswap /dev/xvdm, add it to fstab, sudo swapon -a, and you're good to go. I have done this before and it works fine, but it is probably a bit slower than instance store because it goes over the network.

  • Or you might be able to repartition your disk to add a swap partition, though this might require creating a new AMI. I have not been able to do this in a running instance, because I cannot unmount the root file system, and I do not even have access to the disk device (/dev/xvda), only the partition (xvda1).

  • Or you can create a swap file. This is my preferred solution right now.

    sudo dd if=/dev/zero of=/var/swapfile bs=1M count=2048 &&
    sudo chmod 600 /var/swapfile &&
    sudo mkswap /var/swapfile &&
    echo /var/swapfile none swap defaults 0 0 | sudo tee -a /etc/fstab &&
    sudo swapon -a
    

    Done. :) I know a lot of people feel icky about using files instead of partitions, but it certainly works well enough as emergency swap space.

Solution 2:

Note: Amazon has changed their pricing policy, and does not charge for I/O requests as of mid-2016. The answer is kept here for historical reasons, but there are no cost implications of using (or not using) swap on EC2 EBS-backed instances.


This is by design. Swap is turned off by default on EC2 EBS-backed instances, to avoid unpredictable costs.

If you have a memory-hungry app that goes rogue (say, on a tiny or small instance), it can generate quite a large amount of I/O requests on your EBS volume. Amazon charges $0.10 per 1 million I/O requests (see http://aws.amazon.com/pricing/ebs/).

Under normal conditions you shouldn't worry about it; usually the cost of I/O requests - even on smaller instances - is only a few dollars. So if you know you have a properly sized instance where the swap will be used infrequently, go ahead and enable it. But be careful with tiny instances.

If you enable swap, you might want to keep an eye on Usage Reports. Optionally you can also set up a Billing Alert by going to CloudWatch Control Panel and creating a new Alarm for the total billed amount. This way you'll be notified right away is something weird is going on with your instances.

Solution 3:

The best location for swap IMHO is the instance-store. Why? AWS doesn't charge you for i/o on the instance-store. Besides, the instance-store is more performant than EBS in many cases. Just make sure you have a script that recreates the swap file in case you stop the instance. Reboots are fine. Why oh why it's not there by default?

Let's locate the instance-store.

root@domU-**-**-**-**-**-**:/var/log# fdisk -l

[...]

Disk /dev/xvda2: 160.1 GB, 160104972288 bytes
255 heads, 63 sectors/track, 19464 cylinders, total 312705024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/xvda2 doesn't contain a valid partition table

Hurray, 160.1GB for free! Put your swap in there and forget 100$ overages per server when your EBS-based swap gets swarmed by mistake. Unfortunate experience talking here.

Apparently in some cases you don't see the instance-store.

Depending on the instance type, you first need to attach the instance store volumes to the instance by using the block-device-mapping options. If you don't do this, you may not even see the devices under /dev (as per How to use "Instance Store Volumes" storage in Amazon EC2?)

Solution 4:

Check the /etc/fstab file, they probably were set up without swap in the image you're using. I think some people run without swap for servers since they expect never to use more than the total memory - swapping makes everything super slow.

However, I'm always paranoid about some process ballooning up in memory, so I think it would be prudent of you to simply set up a swap drive and recreate an image from the running ec2 instance.