top of page

CloudWatch Custom Metrics for EC2 Linux Instance

  • Writer: Parag
    Parag
  • Nov 12, 2020
  • 2 min read

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.

ree

Under Basic Details:

  • Security group name: CW-SG

  • Description: Security group for cloud watch

ree

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

ree

Click on Create security group button.

ree

Check the created group.

ree

Step 2: Go to Roles in IAM services.

Click on Create Role.

ree

Select AWS service type of trusted entity.

Choose EC2 use case.

Click on Next: Permissions.

ree

Search and attach CLoudWatchFullAccess permission policy.

Click on Next: Tags.

ree

Add Key: Name and Value: CWRoleForEC2.

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

ree
ree

Check the created role.

ree

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

ree

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.

ree

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

ree

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

ree

Provide Dashboard name: MyDashboard.

ree

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

ree

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

Click on Configure.

ree

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

ree
ree

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

Click on Create Widget.

ree

Click on Save Dahsboard.

ree

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

Run sudo yum update.

ree

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

ree

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

ree

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

ree

Now test and verify the script is running:

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

ree

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

ree

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

ree

Select Number type and click on Next.

ree

Under Custom Namespaces click on System/Linux.

ree

Click on InstanceId.

ree

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

ree

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

ree

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.


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