AWS CloudWatch
Ubuntu
Cloud Monitoring
Custom Metrics
System Performance

How to setup AWS CloudWatch's agent at Ubuntu to get correct custom metrics like cpu, memory and disk usage

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

To collect CPU, memory, and disk metrics from Ubuntu into CloudWatch, you need the Amazon CloudWatch agent, the right IAM permissions, and a config file that explicitly enables the measurements you care about. The default AWS service metrics do not include host-level memory and disk usage, so the agent is the missing piece.

Install the CloudWatch Agent on Ubuntu

On Ubuntu, install the agent package and make sure the instance or machine has permission to publish metrics:

bash
sudo apt-get update
sudo apt-get install -y amazon-cloudwatch-agent

For EC2, the cleanest approach is an instance role with CloudWatchAgentServerPolicy. On non-EC2 machines, configure credentials through the AWS CLI or another supported provider chain.

Create an Agent Configuration

The agent reads a JSON config file. A minimal example that captures CPU, memory, and disk usage looks like this:

json
1{
2  "agent": {
3    "metrics_collection_interval": 60,
4    "run_as_user": "cwagent"
5  },
6  "metrics": {
7    "namespace": "Custom/Ubuntu",
8    "append_dimensions": {
9      "InstanceId": "${aws:InstanceId}"
10    },
11    "metrics_collected": {
12      "cpu": {
13        "measurement": [
14          "cpu_usage_idle",
15          "cpu_usage_user",
16          "cpu_usage_system"
17        ],
18        "totalcpu": true
19      },
20      "mem": {
21        "measurement": [
22          "mem_used_percent"
23        ]
24      },
25      "disk": {
26        "measurement": [
27          "used_percent"
28        ],
29        "resources": [
30          "/"
31        ]
32      }
33    }
34  }
35}

Save it, for example, as /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json.

Start the Agent with the Config

Use the control utility to load the config and start the service:

bash
1sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
2  -a fetch-config \
3  -m ec2 \
4  -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
5  -s

The -m ec2 mode is for EC2. Other environments can use the appropriate mode and credential setup.

After startup, check status:

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

Verify Metrics and Logs

If metrics do not appear, inspect the agent log:

bash
sudo tail -n 100 /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log

Then confirm in CloudWatch that your chosen namespace contains metrics such as:

  • 'cpu_usage_idle'
  • 'mem_used_percent'
  • 'disk_used_percent or used_percent depending on the collected metric naming'

Give it a few minutes after startup. Agent metrics are not always visible instantly.

Choose Measurements Deliberately

The agent can publish far more than just one CPU and one memory metric. That does not mean you should enable everything blindly. More metrics mean more cost and noisier dashboards.

A good starting set is:

  • total CPU utilization or CPU breakdown
  • memory used percent
  • root disk used percent

Only add per-device disk metrics, swap metrics, or per-core CPU details when you actually need them operationally.

Common Configuration Problems

Incorrect metrics are often caused by one of these issues:

  • missing IAM permissions
  • wrong file path or malformed JSON
  • collecting the wrong filesystem resource
  • running on a machine type where ${aws:InstanceId} is unavailable
  • expecting host memory or disk metrics without explicitly enabling them

Another common mistake is confusing CloudWatch agent metrics with the default EC2 metrics. EC2 gives you instance-level platform metrics, but memory and disk usage from inside the OS require the agent.

Common Pitfalls

  • Installing the agent but never loading a config that enables memory and disk measurements.
  • Using valid AWS credentials that still lack CloudWatch agent publish permissions.
  • Collecting the wrong disk resource and then concluding the percentage is inaccurate.
  • Expecting metrics to appear immediately without checking the agent log and namespace.
  • Turning on far more metrics than needed and creating noisy, expensive monitoring.

Summary

  • Install the CloudWatch agent and make sure the machine has permission to publish metrics.
  • Define an explicit config for CPU, memory, and disk measurements.
  • Start the agent with amazon-cloudwatch-agent-ctl and verify its status.
  • Check the agent logs and CloudWatch namespace when metrics look missing or incorrect.
  • Start with a focused metric set and expand only when you have a real monitoring need.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design