Home AWS CloudwatchAgent Setup Linux with Autoscaling
Post
Cancel

AWS CloudwatchAgent Setup Linux with Autoscaling

In order to monitor memory and disk usage, you need to install Cloudwatch agent in the EC2 instance.

Create IAM role > CloudWatchAgentServerPolicy > and attach it to the instance

Add CloudWatchAgentAdminPolicy and AmazonEC2RoleforSSM

Ubuntu

Execute the folowing commands

1
2
3
4
5
cd /usr/local/src/

wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb

dpkg -i amazon-cloudwatch-agent.deb

Open the file config.json (if there is no such file, create one)

1
2
cd /opt/aws/amazon-cloudwatch-agent/bin/
vi config.json

Copy the following to the config.json file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
        "agent": {
                "metrics_collection_interval": 60,
                "run_as_user": "root"
        },
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "aggregation_dimensions": [["AutoScalingGroupName"]],
                "metrics_collected": {
                        "disk": {
                                "measurement": [
                                        "used_percent",
                                        "inodes_free"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "/"
                                ]
                        },
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        }
                }
        }
}

Execute the following command:

1
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

Restart and enable cloudwatch agent to automatically start on boot

1
2
systemctl start amazon-cloudwatch-agent
systemctl enable amazon-cloudwatch-agent

Once done, wait for few minutes. The disk and memory usage will be available at AWS account » CloudWatch » Metrices » CWAgent » AutoScalingGroupName

Create Cloudwatch alarm by selecting any of the metrics.

That’s it!

This post is licensed under CC BY 4.0 by the author.