DEV Community

Cover image for Monitor EC2 memory and disk with CloudWatch Agent๐Ÿ•ต๐Ÿฝ
DevOps Descent
DevOps Descent

Posted on โ€ข Edited on

5 1 2 3 1

Monitor EC2 memory and disk with CloudWatch Agent๐Ÿ•ต๐Ÿฝ

๐Ÿš€ CloudWatch Agent Setup on AWS EC2: Collect Memory & Disk Metrics ๐Ÿ–ฅ๏ธ๐Ÿ“Š

In this guide, you'll learn how to install and configure the CloudWatch Agent on an EC2 instance to collect custom memory and disk usage metrics.

๐Ÿ“ Step 1: Check EC2 Instance Architecture

  • SSH into your EC2 instance.
  • Run this command to check the architecture:
uname -m

This will return x86_64 or aarch64, depending on your instance type.

๐Ÿ”ง Step 2: Install CloudWatch Agent

Download the CloudWatch Agent as per the AWS Official Docs. If youโ€™re running Ubuntu 24.04 on x86_64 architecture, hereโ€™s how to proceed:

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 service:

sudo systemctl enable amazon-cloudwatch-agent.service

โš™๏ธ Step 3: Configure the CloudWatch Agent for Custom Metrics

Create a configuration file to specify which metrics to collect. Run:

sudo nano /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json

Copy and paste this JSON:

{
  "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": [
          "/"
        ]
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Save and exit.

๐Ÿ”„ Step 4: Restart CloudWatch Agent๐Ÿ•ต๐Ÿฝ

Apply the changes by restarting the service:

sudo systemctl restart amazon-cloudwatch-agent.service

โœ… Step 5: Verify the Agent Status

Make sure the CloudWatch Agent is running:

sudo systemctl status amazon-cloudwatch-agent.service

๐Ÿ”’ Step 6: Create EC2 IAM Role

  • Create a new IAM role for your EC2 instance.
  • Attach the CloudWatchAgentServerPolicy to the role.
  • Assign the IAM role to your EC2 instance.

For a detailed step-by-step guide, check the official AWS docs.

๐Ÿ”ง Troubleshooting

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

๐Ÿ“š Reference Docs

That's it! ๐ŸŽ‰ Your EC2 instance should now be sending memory and disk usage metrics to CloudWatch!

Support if you found this helpful๐Ÿ˜‰

No Money ๐Ÿ™…๐Ÿปโ€โ™€๏ธ just Subscribe

Do check: https://linktr.ee/DevOps_Descent
Youtube: https://shorturl.at/lVi2G
GitHub Gist: https://tinyurl.com/yc5v4kne

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (2)

Collapse
 
ankurk91 profile image
Ankur K โ€ข

Very well documented ๐ŸคŒ

Collapse
 
devops_descent profile image
DevOps Descent โ€ข

Thanks

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

๐Ÿ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay