top of page
Writer's pictureParag

CloudWatch Custom Metrics for EC2 Linux Instance

Updated: Jul 29, 2021

CloudWatch Custom Metrics to monitor Memory and Disk Space Utilization for AWS EC2 Linux Instances.



Learning Objectives:

  1. To collect custom metrics to monitor memory and disk space utilization.

  2. Configure CloudWatch monitoring scripts on EC2 Linux Instance.


Step 1: In AWS console go to EC2 Services -> Security Groups. Click on Create Security Group.

Under Basic Details:

  • Security group name: CW-SG

  • Description: Security group for cloud watch

Under Inbound Rules provide 2 rules as follows:

  • SSH – 0.0.0.0/0

  • HTTPS – 0.0.0.0/0

  • HTTP – 0.0.0.0/0

Click on Create security group button.

Check the created group.


Step 2: Go to Roles in IAM services.

Click on Create Role.

Select AWS service type of trusted entity.

Choose EC2 use case.

Click on Next: Permissions.

Search and attach CLoudWatchFullAccess permission policy.

Click on Next: Tags.

Add Key: Name and Value: CWRoleForEC2.

Click on Next: Review. Review the role and click on Create role.

Check the created role.


Step 3: Create a Linux Instance and assign the Security Group that is created in Step 1.

Click here for help in creating the linux instance.

Add a name tag LinuxforCW.

Check the created instance is running.

Go to Actions -> Security -> Modify IAM role.

In the new window, select the EC2CW role we created in Step 2. Click on Save.


Step 4: In AWS Console go to CloudWatch service. In Dashboard, click on Create Dashboard.

Provide Dashboard name: MyDashboard.

Under Add to this dashboard select Line and click on Next.

Under From which data source would you like to create the widget? select Metrics.

Click on Configure.

In the new window select EC2 -> Per-Instance Metrics.

Search and select CPUUtilization corresponding to the above created Linux Instance Name.

Click on Create Widget.

Click on Save Dahsboard.


Step 5: SSH into the Instance. Run the following commands.

Run sudo yum update.

Run the following command for installation of perl:

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

Run the command to change directory to ec2-user dir:

cd /home/ec2-user/

Run the next command to download the monitoring scripts:

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

Run the next command to configure the monitoring scripts:

unzip CloudWatchMonitoringScripts-1.2.2.zip && \

rm CloudWatchMonitoringScripts-1.2.2.zip && \

cd aws-scripts-mon

Now test and verify the script is running:

./mon-put-instance-data.pl --mem-util --verify –verbose

We collect the custom metrics and send them to CloudWatch:

./mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-avail


Step 6: Go to your CloudWatch Dashboard and Add widget.

Select Number type and click on Next.

Under Custom Namespaces click on System/Linux.

Click on InstanceId.

Select the 3 metrics corresponding to the above created Linux Instance Name and click on Create widget.

Here we see that the 3 custom metrics i.e. Memory Available, Memory Used and Memory Utilization are up and running.

Delete the Linux Instance and CloudWatch Dashboard if you no longer need them.



Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.


842 views7 comments

7 Comments


Prajval Suryaji
Prajval Suryaji
Oct 12, 2023

Amazing and easy to understand.


Like

Poonam Singh
Poonam Singh
Oct 12, 2023

Helpful blog, interesting

Like

Pushkraj Fadnis
Oct 30, 2022

Interesting to learn

Like

Archis Davanpelli
Archis Davanpelli
Aug 31, 2021

Very helpful and easy, also interesting to learn.

Like

Shruti Dhongade
Shruti Dhongade
Aug 31, 2021

Very helpful

Like
bottom of page