Cloudwatch Agent Setup for Disk and Memory Utilizatization

RajatRajat
2 min read

Monitor EC2 Memory and Disk Utilisation using CloudWatch | AWS EC2 Monitoring with Cloudwatch | Monitor Memory and Disk Utilisation using CloudWatch | AWS CloudWatch Demo

Steps:

Step 1: Create an AWS EC2 IAM Role

Select the trusted entity as AWS SERVICE

Use Cases = EC2

Permission's = AmazonSSMFullAccess and CloudWatchAgentAdminPolicy and CloudWatchAgentServerPolicy

Name = EC2-Cloudwatch-Role

Click on Create role.

Step 2: Create a Parameter in Systems Manger

a. Go to the systems manager service

b. Click on the parameter store

c. Click on create parameter

Name = /alarm/AWS-CWAgentLinConfig

Store Value=

{
    "metrics": {
        "append_dimensions": {
            "InstanceId": "${aws:InstanceId}"
        },
        "metrics_collected": {
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            },
            "disk": {
                "measurement": [
                     "disk_used_percent"
                ],
                "metrics_collection_interval": 60
            }
        }
    }
}

Step 3: Create an EC2 Instance

--> Choose AMI = Linux AMI 2

--> During the Ec2 configuration Attach the IAM role created in Step 1

--> Add these commands in the Userdata Section.

#!/bin/bash
wget https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
unzip AmazonCloudWatchAgent.zip
sudo ./install.sh
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:/alarm/AWS-CWAgentLinConfig -s

Check if EC2 Instance has CWAgent Installed or not By running this command in terminal:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

If the Output is something like this that' s means the Agent is Configured

[ec2-user@ip-172-31-3-178 ~]$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
{
  "status": "running",
  "starttime": "2024-01-17T12:07:13+0000",
  "configstatus": "configured",
  "version": "1.300032.3b392"
}

Done..

Reference/Additional Reading:

  1. https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent-New-Instances-CloudFormation.html

  2. https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html

0
Subscribe to my newsletter

Read articles from Rajat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajat
Rajat

I'm a DevOps enthusiast from India, eager to learn and grow. Despite being a fresher, I've gained hands-on experience with AWS, Docker, Kubernetes, RHCSA, and CI/CD, and completed some Udemy courses. I'm always open to new insights and connections in the DevOps community. Let's connect!