Skip to content

Instantly share code, notes, and snippets.

@devopsdescent
Last active December 27, 2024 14:49
Show Gist options
  • Save devopsdescent/eb79aba270fd271d6bfcff08a02f798d to your computer and use it in GitHub Desktop.
Save devopsdescent/eb79aba270fd271d6bfcff08a02f798d to your computer and use it in GitHub Desktop.
Level Up Your Monitoring: Install & Configure CloudWatch Agent for Custom Metrics

CloudWatch Agent Setup on AWS EC2 ⚒️

This guide walks you through installing and configuring the CloudWatch Agent on an EC2 instance.

Step 1: Check your EC2 instance's architecture

  • 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

Step 2: Install CloudWatch Agent

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

Step 3: 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,
    "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

Step 4: Restart the CloudWatch Agent

Restart the service to apply the new configuration

sudo systemctl restart amazon-cloudwatch-agent.service

Step 5: Verify the Agent Status

Check if the CloudWatch Agent service is running

sudo systemctl status amazon-cloudwatch-agent.service

Step 6: Create EC2 IAM Role

  • 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.

Troubleshooting:

  • Verify the configuration syntax before restarting the agent.
  • Check logs for errors:
cat /var/log/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log

Reference docs

@ankurk91
Copy link

Well written

@rchavez-neu
Copy link

Great summary, thank you!

One note: CloudWatch Agent log file might also be located here: /var/log/amazon/amazon-cloudwatch-agent

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