Adding swap to EC2 instance
Amazon EC2 micro instances are really cheap but come with only a small amount of memory and no swap device configured. If you need more memory and don’t want to pay for an instance with more memory you can define and activate a swap device. Here is how to do it.
First, let’s have a look at the current memory characteristics.
As you can see there is no swap device configured. By the way look at the dmesg output to see how much physical memory is allocated to the instance.
The instance got 615MB of RAM whereas the instance description on the Amazon AWS site indicate 613MB. Looks like I got 2MB for free!
Before adding a swap space, a swap device must be created. It can be a raw partition on disk or a file hosted on a filesystem. No need to pay for an EBS drive as I have available space on the system disks provided with the instance so I’ll choose the second option. The disk space of the swap file need to be preallocated before configuring it as a swap device. I’ll use dd to create a 1GB file.
Next step is to configure the swap file as a swap device.
Finally, activate the swap device with swapon.
Let’s verify !
Done. But this change will not be persistant across reboot. To activate this new swap device at boot time, it must be defined in the /etc/fstab file.
It is always a good thing to verify that changes made to the system are working as expected. Take time to reboot your instance to make sure the swap device is on at boot time.
blog comments powered by Disqus