Blog

Monitoring Your Instance with AWS CloudWatch
9 Jan 2020

AWS CloudWatch Tutorial: A Complete Guide 2020

What is CloudWatch?

CloudWatch is a service in AWS used to monitor AWS resources. There are multiple predefined metrics are available using that monitoring can be performed.

In this LAB web are going to monitor a web server’s CPU utilization and disk utilization. And also going to create custom metrics for monitoring RAM utilization.

For this LAB we will need an EC2 instance. Login to your AWS account and launch an EC2 instance. In case you don’t know how to launch EC2 instance then you can go through our previous blogs.

I am on my AWS EC2 dashboard and I have an EC2 instance running.

AWS EC2 dashboard

 

We are going to create a CloudWatch dashboard to monitor the health of this EC2 instance. 

Follow the steps for doing it.

  1. Select the CloudWatch service from the AWS service panel, which will be under Management & Governance section.CloudWatch service
  2. You are on the AWS CloudWatch dashboard.AWS CloudWatch Tutorial: A Complete Guide 2020 1
  3. Select the Dashboard from the left side menu.AWS CloudWatch Tutorial: A Complete Guide 2020 2
  4. Click on Create Dashboard button.
  5. Give the name for the dashboard. For me, I  am giving it like “WebServer-Ohio”AWS CloudWatch Tutorial: A Complete Guide 2020 3 
  6. Click on Create Dashboard button.
  7. Select Widget Type for matrics. I am selecting Line for graphs.   AWS CloudWatch Tutorial: A Complete Guide 2020 4
       
  8. Click on the Configure Button.AWS CloudWatch Tutorial: A Complete Guide 2020 5
  9. From Matric Select EC2 –> Per-Instance Metrics –>
  10. From metrics select checkbox of CPUUtilization, Provide name WebServer-CPU for metrics and click on Create widget.AWS CloudWatch Tutorial: A Complete Guide 2020 6
  11. Your Dashboard will look likeDashboard will look like
  12. Add a new widget with type Number and Select Metrics from EC2 –> Per-Instance Metrics and select the checkbox for DiskReadOps and DiskWriteOps.AWS CloudWatch Tutorial: A Complete Guide 2020 7
  13. Click on Create widget.
  14. And New Dashboard will look likeNew Dashboard will look like
  15. Click on the Save dashboard.

We have seen how we can create a CloudWatch dashboard with predefined metrics. And in the next section, we are going to create custom metrics for monitoring RAM utilization.

For doing this task we have some prerequisites. 

  1. IAM role for providing the permission to EC2 instance for writing data on a CloudWatch.
  2. CloudWatch agents installation inside the EC2 machine so that CloudWatch can read RAM utilization from EC2 instance.

Follow the steps.

  1. Open the IAM service dashboard.
  2. Select role from left side IAM features.left side IAM features
  3. Click on Create Role for creating a new role.
  4. Select Service EC2         AWS CloudWatch Tutorial: A Complete Guide 2020 8
  5. Next: Permission
  6. Search for CloudWatch in the search box. And select CloudWatchFullAccess.AWS CloudWatch Tutorial: A Complete Guide 2020 9
  7. Next: Tags
  8. Add proper tags.Add proper tags
  9. Next: Review
  10. Provide Role Name and DescriptionRole Name and Description
  11. Create a role
  12. Attach this role to running EC2 instance. for that first, open an EC2 dashboard and select running instances from the dashboard.running instances from dashboard
  13. Select EC2 instance. Click on Action –> Attach/Replace IAM role.AWS CloudWatch Tutorial: A Complete Guide 2020 10 
  14. Select the EC2-CloudWatch role from the drop-down menu.AWS CloudWatch Tutorial: A Complete Guide 2020 11
  15. Apply.
  16. Our first prerequisite is completed and we are moving forward for a second that is install cloud watch agent inside EC2 instance. For that take remote access of EC2 instance. If you don’t know how to take remote access then you can read our previous blogs or follow the official website documentation from link https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstances.html
  17. I took the remote access from my machine.AWS CloudWatch Tutorial: A Complete Guide 2020 12
  18. Install dependency packages using the command.
    sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 
  19. Download the CloudWatch agent script using the command.curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O 
  20. Unzip the file.
    unzip CloudWatchMonitoringScripts-1.2.2.zip 
  21. Verify the script using command/home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl –mem-util –verify –verbose 
  22. Execute the script./home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl –mem-util –mem-used –mem-avail 
  23. This command will send the utilization data to CloudWatch. And this command should be run periodically. For that, we are going to create a crontab job that will execute this command after every 1 min interval. Follow the steps for creating a cron job.
    • open crontab file for editing using vim editor
      sudo vim /etc/crontab
    • Press i for insert mode and add the following entry.Press i for insert mode
    • Save and exit from the file. for that press, Esc key then type :wq and enter
      Esc :wq
  24. Open the AWS CloudWatch dashboard again.
  25. Select the dashboard and the WebServer-Ohio dashboard that we had created in the previous section.WebServer-Ohio dashboard
  26. Click on Add Widget.
  27. Select Number as a widget type and Configure.
  28. You will get custom namespaces under which is custom metrics. That means Script ran successfully.
  29. Select Linux System — > Instance ID
  30. Select all metrics and type name for metrics then click on Create Widget.Select all metrics
  31. On the same dashboard, you will get a new widget for RAM utilization.
  32. Don’t forget to save it.

In this Lab, we had seen

How to create a CloudWatch dashboard for monitoring with predefined as well as custom metrics. 

Using the same method you can create a dashboard for ELB, S3, EBS and all other services.