AWS CloudWatch Agent installation and configuration steps

Mohita Prakash
5 min readMay 1, 2021

If you are looking for a capsule like documentation to learn how to install CloudWatch agent, get it configured and start it, then you are at right place. I’ll briefly explain about the CloudWatch and then will directly hit the steps to install.

AWS CloudWatch is one of the managed service from Amazon Web Services. It is used to monitor our AWS resources metrics varying from the CPU utilization, Disk read/writes etc. We can create alarms that watch these metrics and send notifications to email or trigger some events which can automatically handle the situation. Some metrics are measured by default and doesn’t add to your pricing. The pricing is usually decided based on the frequency of monitoring. For advanced monitoring the interval in which the data/logs the CloudWatch collects will be shorter. CloudWatch can collect the logs from the application in our EC2 instance. We can group these logs in to Log groups based on its similarity. Later we can export these logs to S3 or upload it wherever we need. For CloudWatch to collect this logs, it need a agent in EC2 instance which is pointed to these logs. This agent is called CloudWatch Agent. We usually install this agent in EC2 instance. In this article I will show you how to install this agent, configure it and start it to collect the logs we simulates from EC2.

To read more about CloudWatch Agent, please check below link: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html

  1. Create EC2 instance

I assume that you have enough knowledge on EC2 instance and how to create it. If not, please go to aws console and search for EC2 and create a new instance by choosing Amazon Linux 2 as AMI. Because the command which we are going to use will work only on Amazon Linux 2 instance. At the end of this article, I’ll mention how to install the agent in other type of instance.

2. Create IAM role for EC2 instance

For accessing CloudWatch from EC2, we need to give permission for the EC2 instance. For this attach CloudWatchAgentServerPolicy to the role which we create.

· Search for IAM

· From left navigation, select ‘Roles’

· Click on ‘Create role’

· Select ‘AWS Service’ and ‘EC2’ as we are creating role for our instance

· Select the policies to attach to this role

· Select ‘CloudWatchAgentServerPolicy’ and ‘CloudWatchFullAccess’

· Add some tag to the role ec2-cw-role

· Save the Role

· Go to EC2 instance and select ‘Modify IAM role’ and add this role to the EC2 instance

3. Connect to the EC2 Instance

Select the EC2 instance and Connect from EC2 Connect or puttty or git bash

4. Install CloudWatch Agent

a) Let’s install the agent in the /opt directory of our instance

b) Make sure you have the /opt directory

ls -al /

c) Change directory to /opt

cd /opt

d) Make a directory to store the logs we simulate and enter to it

sudo mkdir dummylogscd dummylogs

e) Create a new file and add some logs to it

sudo touch myapp.logsudo nano myapp.log

f) Paste the below content or enter any other content

“This is to practise CloudWatch agent.

This simulates some dummy logs.

We can see the logs in CloudWatch log groups”

Ctrl+X -> Yes -> Enter to Save the file

cd .. come out of dummylogs

g) Install the CloudWatch Agent

sudo yum install amazon-cloudwatch-agent

h) Set up the configuration wizard and answer the following questions

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

You can enter the same config or whichever way you want to configure the agent.

When it ask for Do you want to monitor any log files? , enter yes. Then it will ask for the path of your log file . Our path is /opt/dummylogs/myapp.log. So enter this in the path

We can change this configuration by opening the config.json file locally.

Please check the above content of the config.

The config file is also located at /opt/aws/amazon-cloudwatch-agent/bin/config.json.

Edit it manually if needed.

5. Check the status of the agent

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

{

“status”: “stopped”,

“starttime”: “”,

“configstatus”: “not configured”,

“cwoc_status”: “stopped”,

“cwoc_starttime”: “”,

“cwoc_configstatus”: “not configured”,

“version”: “1.247347.4”

}

You can see the status is “stopped”. So we need to start the agent. Please enter the below command to start the agent.

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json

…/opt/aws/amazon-cloudwatch-agent/bin/config.json . This is the path of our config file.

You will get a message, if the client has started successfully.

Configuration validation first phase succeeded

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml

Configuration validation second phase succeeded

Configuration validation succeeded

To confirm, check the status again

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

{

“status”: “running”,

“starttime”: “2021–05–01T07:06:36+0000”,

“configstatus”: “configured”,

“cwoc_status”: “stopped”,

“cwoc_starttime”: “”,

“cwoc_configstatus”: “not configured”,

“version”: “1.247347.4”

}

Hurray..!!! It is up and running.

6. Check the logs in CloudWatch aws console.

Search for CloudWatch

Go to log groups and refresh. You can see the path of our log file or the filename

You can do nano and make changes to the file and it will appear on the CloudWatch.

I hope this article helped you in configuring the CloudWatch agent and gave you a brief idea of its functionalities. Stay tuned for more articles related to AWS.

If you liked this article, please don’t hesitate to give a clap. Let it reach the people who needs help on this topic.

Stay Safe and Let’s learn together..!!!

Photo by Luke Chesser on Unsplash

--

--

Mohita Prakash

Mobile Application Engineer | Talks about tech, finance and fitness | Believes in simplicity | Day Dreamer