Adding swap space to a running EC2 instance06 Jan 2021
Sometimes you want to run some trivial software on an EC2 instance. You don’t really pay attention to what the requirements are or have any performance concerns. So you end up spinning up something with very minimal resources. The software hums along nicely and you think nothing of it. It runs for several days, unnoticed. Then you realize the software stops running for some reason requiring you to restart it. Since you somewhat need the software to run, it becomes a bit annoying. Not annoying enough for a full out solution, but annoying enough you want to have to stop intervening.
Sometimes the lack of resources becomes a problem, especially in the memory department. Instead of spending more money to fix this, a quick and easy solution is to throw some swap on there to test it out. By default EC2 instances do not have swap enabled (for good reason), but since you only want to test things out before committing, it’s quite easy to add swap on the fly.
- First confirm that there is no swap available by using the
$ free total used free shared buff/cache available Mem: 467100 291896 5172 788 170032 88392 Swap: 0 0 0
- Then prep a swap file, here is a 1gb file, and then turn it on.
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 sudo /sbin/mkswap /var/swap.1 sudo chmod 600 /var/swap.1 sudo /sbin/swapon /var/swap.1
- Confirm that the OS sees the swap file by running the
$ free total used free shared buff/cache available Mem: 467100 293124 5200 728 168776 87424 Swap: 1048572 256 1048316
If all goes well, and your software was in fact failing because of a memory issue, the OS should start to use this swap file when it starts to run out of memory.
This isn’t a recommended permanent solution, but a easy way to find out if memory is the root of your issue. If it is, I’d recommend allocating more larger EC2 machine, or permanently enabling swap.