A Cloud Architect Company
How-to-setup-Memory-RAM-and-diskspace-monitor-for-EC2-instance-in-AWS-CloudWatch
Amazon Web Services

How to setup Memory (RAM) and diskspace monitor for EC2 instance in AWS CloudWatch

Monitoring the performance of your EC2 instances in AWS is crucial for ensuring that your applications run smoothly. One important aspect of monitoring is keeping an eye on the memory (RAM) and disk space usage of your instances. In this blog post, we will show you how to set up memory and disk space monitoring for your EC2 instances using AWS CloudWatch.

By following these steps, you will be able to track the usage of these resources in real-time and receive alerts when usage exceeds a certain threshold. This will help you identify potential issues and take action before they become critical, ensuring that your applications continue to perform at their best.

Monitoring RAM and diskspace will prevent from server down & server crash so that now we are going to learn how to monitor RAM and Diskspace for our EC2 instance in a very simple way.

Supported systems

This monitoring scripts will be supported on the below Linux flavours

  • Amazon Linux 2

  • Amazon Linux AMI 2014.09.2 and later

  • Red Hat Enterprise Linux 6.9 and 7.4

  • SUSE Linux Enterprise Server 12

  • Ubuntu Server 14.04 and 16.04

Create an IAM role for the EC2 instance

Go to AWS console, open IAM service → under roles → click create role

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch role

In the trusted entity type, make sure you are in AWS service then select EC2 under use case and click next

aws serviceEC2next

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch use case

Now click create a policy to write our custom policy with cloud watch permissions

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch create policy

click JSON to write code

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch json

Just remove the existing lines under the JSON section

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch remove json

And paste the following JSON code under the JSON section and click next → next

{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:GetMetricStatistics",
"cloudwatch:ListMetrics",
"cloudwatch:PutMetricData",
"ec2:DescribeTags"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch json code

Now give the name for our new policy and then click create policy

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch policy name

Go back to our previous tab, just refresh the page and search for your newly created policy by entering the name, which was you given in the previous step

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch search policy

Click Enter, select our policy, and click next

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch select policy

Just give the name for our role and click create role

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch role name
setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch create role

Now we have successfully created a new role for our EC2 instance with cloud watch permission

Attach the role to our EC2 instance

Open the EC2 service, select your instance → click Actions

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch ec2 action

Click security → modify IAM role

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch modify IAM

Now just select our role and click update IAM role

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch select IAM

Install the required packages on the EC2 instance

for Amazon Linux 2 :

sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64

for Ubuntu :

sudo apt-get update

sudo apt-get install unzip

sudo apt-get install libwww-perl libdatetime-perl

for Red Hat Enterprise Linux 7 :

sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA --enablerepo="rhui-REGION-rhel-server-optional" -y 

sudo yum install zip unzip

for SUSE :

In SUSE Linux Enterprise Server 12, you need to download the perl-Switch package

wget http://download.opensuse.org/repositories/devel:/languages:/perl/SLE_12_SP3/noarch/perl-Switch-2.17-32.1.noarch.rpm

sudo rpm -i perl-Switch-2.17-32.1.noarch.rpm

Now install packages :

sudo zypper install perl-Switch perl-DateTime

sudo zypper install –y "perl(LWP::Protocol::https)"

Install monitoring scripts on EC2

To install the monitoring script, use the below command

curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch install script

unzip and remove the monitoring script zip file

unzip CloudWatchMonitoringScripts-1.2.2.zip && rm CloudWatchMonitoringScripts-1.2.2.zip

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch remove zip file

Change the directory to aws-scripts-mon you can see the below script files

cd aws-scripts-mon
ls

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch list

When we run the “ mon-put-instance-data.pl “ script file, it will send memory and disk space reports to the cloud watch

Schedule cron for monitoring script

if we schedule cron it will automatically run our monitoring script each time as we mentioned in the crontab, and our script will send report to cloud watch.

open crontab using below command

crontab -e

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch crontab

Now crontab file will be opened, just replace your script path with the below command and then copy & paste the command in our crontab file,

and then save & exit.

*/1 * * * * <path-to-script>/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --disk-space-util --disk-space-used --disk-space-avail --disk-path=/

*/1 * * * * /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --disk-space-util --disk-space-used --disk-space-avail --disk-path=/

If we give cron like this “ */1 * * * * ” it will run our script at every single minute

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch crontab file

Crontab Issue

If you are facing “Command not found” issue when using crontab , then follow the below steps to rectify this error.

Install cron

yum install cronie cronie-anacron

Now check using “crontab -e” command , it will works

Setup Cloud Watch metrics

Go to cloud watch service → Dashboards → create dashboard

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch create dashboard

Enter the name for our dashboard and then click create dashboard

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch dashboard name

Select the type of widget you want

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch add widget

Select system/linux metrics under custom namespace

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch namespace

click “ Filesystem, InstanceId, MountPath “

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch metrics

Copy your instance id and filter out your instance by search

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch instance id

Filter and select all the three metrics of your instance and click create widget

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch instance metrics

Enter the Gauge range 0 – 100 and click create widget

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch gauge range
Now under Dashboards → click our newly created dashboard , and then click the “ + “ symbol at the right corner to add the balance metrics
setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch disk

Again select type of widgetsystem/linux metrics → click Instanceid

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch metrics

Filter out using our instance id and select all the three metrics of your instance and click create widget

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch select instance metrics

Enter the Gauge range 0 – 1000 and click create widget

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch gauge range

Now change the custom time to 1 minute , then only all the changes in our instance RAM & memory usage will be reflect in our dashboard and then → click save

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch save

We have successfully done !! our cloud watch monitoring setup for Memory(RAM) & Disk space , here after we can easily monitor our EC2 instance’s memory and disk usages.

You can check if our cron is working or not , by using below command

cat /var/log/cron

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch cron command

setup Memory(RAM) and diskspace monitor for EC2 instance in AWS CloudWatch cron logs

yeah!! our cron is working properly .

Article written by:

Junior DevOPs Engineer, working @ Easydeploy Technologies Pvt Ltd, Coimbatore, with 1 year of experience, and currently Doing server configurations by automation using Ansible & building strong AWS resources using Terraform, and creating a containerized application using Docker.

Join the discussion

  1. Rafael

    Thanks for post, help me! Thx!

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top
advanced-floating-content-close-btn

Contact Us to save your AWS bill by 40%

X