Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I started a couple servers on EC2 and they don't have swap.

Am I doing something wrong or is it that the machines just don't have any?

share|improve this question
    
I wondered that too, but I just set up an EBS instance, formatted it as swap, and swapon /dev/sdg... –  Tom O'Connor Jan 4 '11 at 22:21
    
Its also typical, the case of using SSD drives on a Linux system to not setup swap on the SSD drive. Mostly because some people are paranoid it would have a negative impact on the storage life of a SSD by drilling the same set of sectors all the time. –  djangofan Jan 5 '11 at 20:08
    
What AMI and which EC2 instance size. The AMI needs to be configured to use a swap partition and the instance has to have it added when started up. –  Jeremy Bouse Jan 5 '11 at 22:19
    
If at all possible, I'd advise not to use swap on EC2 unless you're 99% certain you won't have to use it (I.E. it's only there for emergency). When we disabled swap on some of our EC2 instances our monthly EBS IO costs probably halved. Just my two cents to save you two cents - yes that was terrible, I apologize and will go hide in a corner ;) –  sam Jun 12 '11 at 23:29
add comment

4 Answers

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, and it is probably a bit faster than using a swap file, but for a server that doesn't normally depend on swap performance, I personally think the minor performance improvement is not worth the added complexity of having to attach a volume. (Update: It's probably not faster than a swap file on instance storage, since EBS has become known for lousy and unpredictable performance.)

  • 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.

share|improve this answer
    
Swapping to an EBS volume may result in additional costs, as indicated by the other answers. Not using swap, or swapping to an instance store seem like better solutions. –  briankb Jan 7 '13 at 7:15
1  
Using the instance store is a better option than EBS. EBS is a network file system, so connects to the EC2 instance over the same network connection as everything else. Instance store is attached to the hardware that is hosting the instance (that is why it does not persist when you stop and start your instance). The only time that EBS is an option for swap is when you are using an instance type that does not come with instance storage ie., the t1.micro (which given it only has about 620MB RAM is probably the one that really does need emergency swap). –  ColtonCat Jan 21 '13 at 23:38
    
The bs=1M already is in binary notation, so the count=2048 multiplier should be count=2000 - if I'm not wrong. –  ypocat Jan 24 '13 at 22:28
    
If you're going for 2GiB, then 1024*2048 seems correct. –  Jo Liss Jan 25 '13 at 13:29
add comment

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?)

share|improve this answer
add comment

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 a few dollars, if so. So if you know you have a properly sized instance and that swap will be used only 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.

share|improve this answer
add comment

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.

share|improve this answer
1  
Occasional swapping does not make the system slow. –  laebshade Jun 13 '11 at 0:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.