92

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?

mattdm
  • 6,550
  • 1
  • 25
  • 48
rafamvc
  • 1,119
  • 1
  • 8
  • 7
  • I wondered that too, but I just set up an EBS instance, formatted it as swap, and swapon /dev/sdg... – Tom O'Connor Jan 04 '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 05 '11 at 20:08
  • 1
    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 05 '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 ;) – Smudge Jun 12 '11 at 23:29
  • You could also look in this steps http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-store-swap-volumes.html#step_mkswap – Artem.Borysov Dec 07 '15 at 18:05

5 Answers5

81

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.

Jo Liss
  • 2,818
  • 1
  • 22
  • 23
  • 5
    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. – blee Jan 07 '13 at 07:15
  • 9
    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
  • 1
    If you're going for 2GiB, then 1024*2048 seems correct. – Jo Liss Jan 25 '13 at 13:29
26

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.

Gui Ambros
  • 1,349
  • 2
  • 13
  • 17
  • This answer is a bit misleading or confusing to me since the most common instance types use SSD volumes. https://aws.amazon.com/ec2/instance-types/ – Taylor D. Edmiston Aug 08 '16 at 14:40
  • 2
    @tedmiston: indeed, this answer isn't applicable anymore. I've adjusted to reflect the new reality, but basically there's no charge for I/O requests, so enabling swap (or not) can now be made on a pure technical basis. – Gui Ambros Aug 09 '16 at 00:04
  • And judging from the question, this actually answers it. – volvox Dec 12 '19 at 03:51
25

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

sebwinadmin
  • 505
  • 2
  • 6
  • 11
1

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.

ehsanul
  • 427
  • 1
  • 8
  • 19
0

A simple solution to running swap in EC2 images is to run swap compressed with lz4 in ram with zram-init.

This solution does not take ram away from the host:

enter image description here

Stuart Cardall
  • 531
  • 4
  • 7
  • 4
    Very creative, but wouldn’t using your RAM as actual RAM make more sense than using it as an in-memory file-system for swap? (which primarily gets used when you run out of memory) Reducing the amount of available memory for applications to create swap seems counterintuitive... – HBruijn Aug 24 '18 at 08:25
  • Interesting answer, but I think using EBS / instance store is probably a better general purpose solution. I have a t2.nano with 512MB RAM and 512MB swap on EBS that works fine. – Tim Aug 24 '18 at 08:40
  • 1
    This clearly does take RAM from the host (EC2 instance) - where else is the RAM coming from? The only benefit of this approach is to compress some of your total RAM, but it doesn't provide the ability to use SSD as swap. You could instead use `zswap` which provides compressed RAM cache on top of a normal swap file/disk: https://wiki.archlinux.org/index.php/zswap. See https://www.cnx-software.com/2018/05/14/running-out-of-ram-in-ubuntu-enable-zram/ for more on zram. – RichVel Feb 19 '19 at 06:19