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.
Related videos on Youtube
rafamvc
Updated on September 17, 2022Comments
-
rafamvc over 1 year
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?
-
Tom O'Connor over 13 yearsI wondered that too, but I just set up an EBS instance, formatted it as swap, and swapon /dev/sdg...
-
djangofan over 13 yearsIts 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.
-
Jeremy Bouse over 13 yearsWhat 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.
-
Toby Mao almost 13 yearsIf 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 ;)
-
Artem.Borysov over 8 yearsYou could also look in this steps docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
-
-
laebshade almost 13 yearsOccasional swapping does not make the system slow.
-
hpcodeit over 11 yearsSwapping 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.
-
ypocat over 11 yearsThe bs=1M already is in binary notation, so the count=2048 multiplier should be count=2000 - if I'm not wrong.
-
Jo Liss over 11 yearsIf you're going for 2GiB, then 1024*2048 seems correct.
-
Henrik Andersson almost 10 yearsjust want to point out that t1.micro users on free tier can't use this option.
-
Taylor D. Edmiston almost 8 yearsThis answer is a bit misleading or confusing to me since the most common instance types use SSD volumes. aws.amazon.com/ec2/instance-types
-
daniele almost 8 years@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.
-
HBruijn over 5 yearsVery 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...
-
Tim over 5 yearsInteresting 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.
-
RichVel over 5 yearsThis 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: wiki.archlinux.org/index.php/zswap. See cnx-software.com/2018/05/14/… for more on zram. -
volvox over 4 yearsAnd judging from the question, this actually answers it.