AWS AMI swap add on

With t3a.nano instance you got 512MB at a very reasonable price per month (around $5 /  4.5€ for an on-demand instance). It should be enough for running a micro wordpress blog with docker, but sometime it is not. A pair of docker containers can eat up all the memory and your little box will be unable to launch docker-compose shutdown commands with out of memory error all way around.
But we are here to solve the issue, and get the best bang for your bucks :)

To solve temporary memory failures, just create a nice swap space:

sudo bash
# The following lines as root
fallocate -l 1G /jj-swapfile
# if fallocate did not exist, use the old one below
# dd if=/dev/zero of=/jj-swapfile bs=1024 count=1048576
chmod 600 /jj-swapfile
mkswap /jj-swapfile
swapon /jj-swapfile

fallocate will create the file but avoid to initialize it, hence it is faster the old style “dd”.

Because using a block storage as swap file can slow down a lot your little box, do some tests before enabling it.

Anyway this trick enable your system to better manage workload spikes


UPDATE July2020: The same trick can be used on Azure VMs, even on the smallest one