This guide walks you through installing and configuring the CloudWatch Agent on an EC2 instance.
- SSH to your EC2 instance
- Run the following command to determine your EC2 instance's architecture:
uname -m
This command will output the machine architecture, for example: x86_64
or aarch64
Download the cloudwatch agent from the AWS Official Docs according to your platform.
Assuming that you are running Ubuntu 24.04
with x86_64
architecture on your EC2
cd ~
wget https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo apt install ./amazon-cloudwatch-agent.deb
rm -f ./amazon-cloudwatch-agent.deb
Enable the newly installed service
sudo systemctl enable amazon-cloudwatch-agent.service
Run the following command to create a new configuration file:
sudo nano /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json
and paste the given JSON code into the nano editor
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"aggregation_dimensions": [
[
"InstanceId"
]
],
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
]
},
"disk": {
"measurement": [
"disk_used_percent"
],
"resources": [
"/"
]
}
}
}
}
Save the file and exit from nano editor
Restart the service to apply the new configuration
sudo systemctl restart amazon-cloudwatch-agent.service
Check if the CloudWatch Agent service is running
sudo systemctl status amazon-cloudwatch-agent.service
- Create a new IAM role for your EC2.
- Attach
CloudWatchAgentServerPolicy
policy to the IAM Role. - Now attach the newly created IAM role to your EC2.
Follow the official aws guide for detailed steps.
That's it. Your EC2 instance must start sending custom metrics to CloudWatch now.
- Verify the configuration syntax before restarting the agent.
- Check logs for errors:
cat /var/log/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log
Well written