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?
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?
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.
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.
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.
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.
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.
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.
A simple solution to running
swap
inEC2
images is to runswap
compressed withlz4
inram
withzram-init
.systemd
&openrc
.This solution does not take
ram
away from thehost
: