Amazon EC2: Optimizing Your Instance Usage by adding Swap Space.

Amazon EC2: Optimizing Your Instance Usage by adding Swap Space.

Profit maximization is one of every business's primary goals, and one simple way to achieve this is to assess and reduce operating costs. Therefore, a capable Solutions Architect should be skilled at assessing and optimizing solutions and infrastructure for both cost and performance.

If you're an aspiring Cloud Architect/Engineer and an AWS free-tier user or someone looking to get extended performance out of AWS compute service, EC2,  you'll find this a very rewarding read. Let's get into it!

To avoid exceeding the limits of your account budget (which I hope you have set up) while staying within the free tier, you could try to use a single EC2 instance to deploy several services, such as hosting a blog and running one or more Docker containers hosting other applications. Currently, AWS's free tier only provides 750 compute hours per month, which means that if you don't want to exceed this tier, you can only deploy 1 instance with 1 vCpu, most likely a t2.micro instance, which, while capable, only provides 1GB of memory (RAM) that can be easily consumed by multiple running processes.

Solution - Adding swap space: Extending your memory (per se)

If you're running a Linux instance, you can use swap space to allocate space on your hard disk (EBS volume in this case) for extra memory, though this is much slower than regular memory. Swap space is used to provide virtual memory (a combination of RAM and disk space) to running processes. Consider it a portion of hard disk space that allows the computer operating system to pretend it has more memory.

Note: The scope of this blog post only covers Linux-based EC2 Instances.

To begin, we ssh into our ec2 instance using our private key and use the command line to verify the amount of free and used memory on our instance:

$ ssh -i private_key.pem username@instance_public_ip -y

$ free -h

Depending on the type of instance and number of processes running, we can improve memory performance by creating a swap file using the steps given below;

  • Use the following command to create a swap file with the name "swap.1"

$ sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024

  • Set permissions on the swap file so that only the root user may write and read it

$ sudo chmod 600 /var/swap.1

  • Use the mkswap utility to set up the file as a Linux swap area

$ sudo /sbin/mkswap /var/swap.1

  • Enable the swap with the following command

$ sudo /sbin/swapon /var/swap.1

  • To make the changes persist on system reboot, edit the /etc/fstab file and add a line to it

$ sudo nano /etc/fstab

  • Add this line

/var/swap.1 swap swap defaults 0 0

Great! By reusing the previous command to check our memory consumption, we can now see if our new swap space is active.

$ free -h

To illustrate, adding swap space to the instance below, which is used to host a blog and run numerous Docker containers, helps to lessen the amount of stress on the system's main memory, preserve performance and prevent system freeze/slowdown. Using the free command, we see the effects below.

I hope you found this information useful. Thank you for reading. :)