Open In App

Collect Metrics and Logs with the CloudWatch Agent

Improve
Improve
Like Article
Like
Save
Share
Report

Amazon CloudWatch is the powerful tool that used to collect metrics on resources and monitor their performance of the resources. It can monitor system-level metrics like CPU usage, memory usage, and disk space and also the application levels metrics like requests per second and error rates. By collecting these data it will help the developers to analyze and know the performance of the servers.

Tasks Performed by Cloudwatch Agent

  • It collects,monitors and stores the log files.
  • It collects and tracks key metrics
  • It can also create alaram and sends notifications regarding the errors.

How does the Cloudwatch Agent Works?

It collects all the metrics and logs from aws resources and applications. With the help of these collected metrics it helps us to visualize the application on cloudwatch dashboard.If we do some operational changes in the environment the cloud watch agent becomes aware of these changes and act accordingly.And finally it provides us real time analysis by using cloud watch metric map.

Setting up Cloud Agent in Our Servers

Step 1: Firstly login to your AWS account and we need to create the roles. Search for IAM role and navigate to it and on left side under Acess managment go to roles and click on create new role and keep the trusted identity as default and in use case select EC2 as shown in the image below.

 

Next, search for these three roles as mentioned below and select them

  1. AmazonEC2RoleforSSM
  2. CloudWatchAgentAdminPolicy
  3. CloudWatchAgentServerPolicy
  • After selecting the polices click on next and give the role name as you wish and click on create role.It takes sometime and you can see the role you created under your roles.

Step 2:Creating Instance

  • Click on launch instance and name your instance and here I am using the default one i.e  Amazon Linux OS and choose the instance type as you wish and select the key pair if you have already created or else create a new keypair and set the remaining things as default and click on launch instance and after sometime in your EC2 dashboard you will see the instance running and make sure it passes all the checks. And now select the instance and click on actions on top right corner and hover over security and choose Modify IAM role and now choose the role you have created before if doesn’t appear click on refresh and it will be visible now and click on Update IAM Role.
  • Now click on connect and in EC2 Instance Connect at bottom click on connect it will be open in new tab and run ‘sudo su -‘ for changing to root user and run ‘yum update -y’ for updating the system and after run ‘sudo yum install amazon-cloudwatch-agent -y’ for installing the cloudwatch on our server.
  • Now we need to navigate to the folder where the cloudwatch is installed for that run the coomand  ‘sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard’ and it will ask some questions respond according to your usage after that you will the options you have selected in the json format  and now for confirming hit enter and after 2-3 questions it will ask for the log file path there enter the path as follows ‘/var/log/messages’ and after you need to give the name that you want to see after  going to logs option and after select the choices according to your requirements.
  • And after seeing the command program exists navigate to the folder where cloudwatch agent is stored for navigating run the command ‘cd /opt/aws/amazon-cloudwatch-agent/bin’ and now run ‘ls’ and if you have sucessfully rconfigured the cloudwatch agent you should see the ouput as shown below 

 

  • Now we need to start the cloudwatch agent by running the command ‘sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json’. 

Step 3:Collecting Metrics and Logs

  • Navigate to Cloudwatch agent and on the left side you will see metrics and click on all metrics there we will see our EC2 instance and click on  Per-instance metrics and scroll down you will see the EC2 instance name you used for the instance as the image given below

 

  • Here in the below image I have shown the CPUutilization if you want to see the other metrics just check the boxx side to it you will be able to see them on the above.

Example view of CPU utilization

 

  • And if we comeback onestep i.e to Browse tab and in EC2 if we click on View automatic dashboard it will give about all the information of our server.Refer below image foe refernce.

 

  • And for viewing logs same on the left side go to the log groups you can view your logs there 


Last Updated : 01 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads