How to Collect Memory Disk Usage By Using CloudWatch Agent

2021/3/20·2 min read

This blog provides a quick guide on how to set up CloudWatch Agent manually on EC2 Linux Instance, CloudWatch Agent enables you to collect system or custom metrics from Amazon EC2 instances across operating systems. You can install CloudWatch Agent on AWS Cloud or on-promise server, Linux, or Windows as well. 

Install the CloudWatch Agent on Amazon Linux EC2

sudo yum install amazon-cloudwatch-agent

You can import the CloudWatch agent if you have already, that'll be useful if you want to re-use the same CloudWatch agent config over and over again. Following is the step to use the default wizard to create a new config file.

Run the CloudWatch Agent Wizard

Once you run the following command on Linux EC2, you'll get started with the CloudWatch Agent wizard. 

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

CloudWatch Agent Config File Path

After you finish the CloudWatch agent setup wizard, it'll create a JSON config file at the following location.

/opt/aws/amazon-cloudwatch-agent/bin/config.json

Install collectd for the CloudWatch Agent

If you wish to use collectd to collect metrics, you should install via the following command.

sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y collectd

CloudWatch Agent File

This CloudWatch agent config file, it used to specify proxy and region information. 

cd /opt/aws/amazon-cloudwatch-agent/etc
nano amazon-cloudwatch-agent.toml 

Run the CloudWatch Agent on EC2 Linux

Following is the command to the CloudWatch agent with config file you either create from the wizard or from your previous config file.

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

Should work now..?

At this point, if everything works out you should start seeing memory or disk usage, so go to the CloudWatch > Metrics, click the All metrics, click the CWAgent, under CWAgent should contain a list of metrics you set up from the CloudWatch Agent, and data is sending from your EC2.