Introduction:

Swap space, an extension of physical memory, enables instances to manage memory resources efficiently, preventing performance degradation in situations of high demand. This guide outlines the steps to add swap space to an Amazon EC2 instance

Step 1:

Connect to EC2 Instance.

Step 2:

Before adding a swap, check the current disk space and available memory on your instance. we can use commands to check disk space and check memory.

$ df -h
$ free -h

Step 3:

Decide on the size of your swap space. For example, if we want to create a 1GB swap file, we can use the following commands.

$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Step 4:

Secure the swap file by adjusting permissions:

$ sudo chmod 600 /swapfile

Step 5:

Use the following command to mark the file as swap space.

$ sudo mkswap /swapfile

Step 6:

Activate the swap file with the following command.

$ sudo swapon /swapfile

Step 7:

Add the swap file to the /etc/fstab file to ensure it’s mounted on the system reboot.

$ echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

Step 8:

Confirm that the swap space is available and active by running.

$ sudo swapon –show

Conclusion:

The addition of swap space provides a pragmatic solution to handle fluctuations in memory demand, ensuring a seamless user experience.

Leave a Reply