Skip to content

Instantly share code, notes, and snippets.

@ankurk91
Last active June 1, 2025 02:49
Show Gist options
  • Save ankurk91/ec8a3d74540e8bc0cc89ae1db019239f to your computer and use it in GitHub Desktop.
Save ankurk91/ec8a3d74540e8bc0cc89ae1db019239f to your computer and use it in GitHub Desktop.
AWS Cloudwatch agent setup on EC2

Install CloudWatch Agent on AWS EC2

Install CloudWatch Agent

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

Configure the CloudWatch Agent to collect custom metrics

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 EC2 IAM Role

  • 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 CloudWatch Agent

Restart the service to apply the new configuration

sudo systemctl restart amazon-cloudwatch-agent.service

Verify the Agent Status

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.

Troubleshooting:

  • 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

Reference docs

@ankurk91
Copy link
Author

reserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment