I started a couple servers on ec2 and they don't have swap. Am I doing something wrong or the machines just dont have swap?

link|improve this question

60% accept rate
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
feedback

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

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

link|improve this answer
feedback

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.

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

Your Answer

 
or
required, but never shown

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