Download the cloudwatch agent from the AWS Official Docs according to your platform.
Assuming that you are running Ubuntu
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
OR If you are on ARM (Graviton)
cd ~
wget https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/arm64/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,
"debug": false,
"run_as_user": "cwagent"
},
"metrics": {
"aggregation_dimensions": [
[
"InstanceId"
]
],
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
]
},
"disk": {
"measurement": [
"disk_used_percent"
],
"resources": [
"/"
],
"ignore_file_system_types": [
"sysfs",
"devtmpfs",
"tmpfs"
]
}
}
}
}
Save the file and exit from nano editor.
- Create a new IAM role for your EC2. Guide
- Attach
CloudWatchAgentServerPolicy
policy to the IAM Role. - Now attach the newly created IAM role to your EC2.
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
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
cat /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
reserved