Firstly, this article mainly applies to low memory Amazon Linux EC2 instance types such as t2.nano and t2.micro. The instructions below are also specific to Amazon Linux AMI, which like CentOS, is based on RHEL (Red Hat Enterprise Linux). Yum!
Update all packages – yum package manager
First, login to your new Amazon Linux EC2 instance and update all packages:
sudo yum update
Then if you are not going to use MTA (mail transfer agent) you can disable sendmail service:
sudo chkconfig sendmail off sudo service sendmail stop
Reduce the number of getty services
Edit /etc/sysconfig/init and replace:
Replace with agetty with mingetty
Update! – AWS now installs mingetty by default. In which case if the output of the install command is “already installed”, then simply make the line change to the /etc/init/serial.conf file as described below.
Install mingetty first:
sudo yum install mingetty
then edit /etc/init/serial.conf and replace:
exec /sbin/agetty /dev/$DEV $SPEED vt100-nav
exec /sbin/mingetty /dev/$DEV $SPEED vt100-nav
Disable yum-updatesd and replace it with a simple cron job.
Update! – AWS no longer installs yum-updatesd by default. You can still setup the cron if you’d like. Check to see if yum-updatesd is installed using this command to list: chkconfig
This will save resident memory.
sudo chkconfig yum-updatesd off
sudo yum remove yum-updatesd
Next, create yum update cron instead. Add a new file /etc/cron.daily/yum.cron with contents:
#!/bin/sh /usr/bin/yum -R 120 -e 0 -d 0 -y update yum /usr/bin/yum -R 10 -e 0 -d 0 -y update
sudo chmod +x /etc/cron.daily/yum.cron
Disable IPv6 support
sudo chkconfig ip6tables off
Disable Network Time Protocol (NTP) daemom
Next, disable ntpd. Run “top” and press shift + M to sort by memory usage, you’ll notice that ntpd is close to the top of the list. Its used to keep your server clock in sync. You can replace with weekly cron so you can disable the service and further reduce memory usage.
sudo service ntpd stop sudo chkconfig ntpd off sudo chkconfog ntpdate off
Now add a new file named ntpdate-sync to the /etc/cron.weekly directory with the contents:
#! /bin/sh /usr/sbin/ntpdate pool.ntp.org
sudo chmod +x /etc/cron.weekly/ntpdate-sync
To test run:
Output should be something like:
11 Oct 22:57:49 ntpdate: adjust time server 188.8.131.52 offset -0.017816 sec
These are some basic steps to lower memory consumption of first boot. More noticeable on the smaller Amazon Linux EC2 instances.
Tuning Amazon Linux EC2 swappiness and cache pressure
Another method of squeezing the most from your Amazon Linux EC2’s limited RAM, is to tune the system’s swappiness (tendency to swap) and cache pressure (tendency to reclaim cache).
swappiness (Recommended value 10 to 60. 0 if you don’t have swap added) – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase agressiveness, lower values decrease the amount of swap. (default = 60)
vfs_cache_pressure (Recommend value 50 to 200) – Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. (default = 100)
Add these lines to the end of the /etc/sysctl.conf file.
Increasing the cache pressure may be somewhat counter productive since caching is good for performance. However, swapping too often can also reduce your server’s overall performance. Use this for example if free -m shows say more than 60% of RAM being used by cache/buffers …remember this NOT a bad thing! However, depending on what is being cached you may reduce memory usage/contention and as a result gain performance by adjusting.
To check current values using these commands:
sudo cat /proc/sys/vm/swappiness sudo cat /proc/sys/vm/vfs_cache_pressure
To enable these settings without rebooting use the following commands:
sudo sysctl -w vm.swappiness=10 sudo sysctl -w vm.vfs_cache_pressure=200
How To Add Swap on Amazon Linux EC2
Of course, on low-memory instances swap is wise. To add a 1GB swap file for example, from command line you’ll type:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Now setup the swap file with the command:
sudo mkswap /swapfile sudo chmod 600 /swapfile
Now enable the swap:
sudo swapon /swapfile
If you use the top command, you should now see the 1gb swap added. So now lets make swap persistent so it’s not dropped when you reboot. Edit /etc/fstab file and add this line as the last line:
/swapfile swap swap defaults 0 0
When you reboot, use the free -h or df -h command to check for swap.
Remember, adding swap can help save your server from running out of memory but if it’s already using a big chunk of swap (aka swapping), that is never good for performance. A lot can be expanded upon with regards to swap and paging/swapping. However, the point today is that stripping/tuning the AMI.
Note: this article was originally published on Nov 21, 2013. It has been updated to ensure that the suggested changes are still compatible.