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.
We are going to create a CloudWatch dashboard to monitor the health of this EC2 instance.
Follow the steps for doing it.
- Select the CloudWatch service from the AWS service panel, which will be under Management & Governance section.
- You are on the AWS CloudWatch dashboard.
- Select the Dashboard from the left side menu.
- Click on Create Dashboard button.
- Give the name for the dashboard. For me, I am giving it like “WebServer-Ohio”
- Click on Create Dashboard button.
- Select Widget Type for matrics. I am selecting Line for graphs.
- Click on the Configure Button.
- From Matric Select EC2 –> Per-Instance Metrics –>
- From metrics select checkbox of CPUUtilization, Provide name WebServer-CPU for metrics and click on Create widget.
- Your Dashboard will look like
- Add a new widget with type Number and Select Metrics from EC2 –> Per-Instance Metrics and select the checkbox for DiskReadOps and DiskWriteOps.
- Click on Create widget.
- And New Dashboard will look like
- 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.
- IAM role for providing the permission to EC2 instance for writing data on a CloudWatch.
- CloudWatch agents installation inside the EC2 machine so that CloudWatch can read RAM utilization from EC2 instance.
Follow the steps.
- Open the IAM service dashboard.
- Select role from left side IAM features.
- Click on Create Role for creating a new role.
- Select Service EC2
- Next: Permission
- Search for CloudWatch in the search box. And select CloudWatchFullAccess.
- Next: Tags
- Add proper tags.
- Next: Review
- Provide Role Name and Description
- Create a role
- Attach this role to running EC2 instance. for that first, open an EC2 dashboard and select running instances from the dashboard.
- Select EC2 instance. Click on Action –> Attach/Replace IAM role.
- Select the EC2-CloudWatch role from the drop-down menu.
- 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
- I took the remote access from my machine.
- 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
- Download the CloudWatch agent script using the command.curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
- Unzip the file.
- Verify the script using command/home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl –mem-util –verify –verbose
- Execute the script./home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl –mem-util –mem-used –mem-avail
- 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 the AWS CloudWatch dashboard again.
- Select the dashboard and the WebServer-Ohio dashboard that we had created in the previous section.
- Click on Add Widget.
- Select Number as a widget type and Configure.
- You will get custom namespaces under which is custom metrics. That means Script ran successfully.
- Select Linux System — > Instance ID
- Select all metrics and type name for metrics then click on Create Widget.
- On the same dashboard, you will get a new widget for RAM utilization.
- 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.