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.
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:
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:
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:
The -m ec2 mode is for EC2. Other environments can use the appropriate mode and credential setup.
After startup, check status:
Verify Metrics and Logs
If metrics do not appear, inspect the agent log:
Then confirm in CloudWatch that your chosen namespace contains metrics such as:
- '
cpu_usage_idle' - '
mem_used_percent' - '
disk_used_percentorused_percentdepending 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-ctland 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.
Related reading
- How to setup Kubernetes NLB Load Balancer with target group IP based AWS?
- How to share an EFS volume across multiple namespaces
- How to simplify aws DynamoDB query JSON output from the command line?
- How to solve -Cannot use import statement outside a module in AWS lambda console
- How to share a file from initContainer to base container in Kubernetes
- How to share rate limiting state between traefik instances?
- How to shorten multiple condition
- How to show the last queries executed on MySQL?
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.