Monitor disk usage EC2 Ubuntu instance

Below are the steps to configurion AWS dashboard and Ubuntu server instanceto send disk usage metrices to AWS.

Go to IAM > Roles and click Create role button:

Select AWS service as Trsuted entity type, EC2 as use case and click Next button.

On the next page, chose CloudWatchAgentServerPolicy policy for that role and hit the Next button.

Name the rolw as EC2CloudWatchAccessRole. You can give any name you want for that role.

Hit Create role button. The role EC2CloudWatchAccessRole will be created.

It’s the time to do the configutaion on your server side. Connect to EC2 instance using SSH. Follow the folllowing reference How to connect to AWS EC2 instance from local MAC machine

After connecting to remote EC2 instance, go to tmp folder:

cd /tmp

Download CloudWatch agent (Reference: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html):

Run the following command in your terminal connected to the server:

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

The terminal will present the output like this:

--2022-08-11 11:44:50--  https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
Resolving s3.amazonaws.com (s3.amazonaws.com)... 54.231.226.152
Connecting to s3.amazonaws.com (s3.amazonaws.com)|54.231.226.152|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 63484608 (61M) [application/octet-stream]
Saving to: ‘amazon-cloudwatch-agent.deb’

amazon-cloudwatch-a 100%[===================>]  60.54M   102MB/s    in 0.6s    

2022-08-11 11:44:50 (102 MB/s) - ‘amazon-cloudwatch-agent.deb’ saved [63484608/63484608]

Install the package:

sudo dpkg -i -E ./amazon-cloudwatch-agent.deb

Output:

Selecting previously unselected package amazon-cloudwatch-agent.
(Reading database ... 149847 files and directories currently installed.)
Preparing to unpack ./amazon-cloudwatch-agent.deb ...
create group cwagent, result: 0
create user cwagent, result: 0
create group aoc, result: 0
create user aoc, result: 0
Unpacking amazon-cloudwatch-agent (1.247354.0b251981-1) ...
Setting up amazon-cloudwatch-agent (1.247354.0b251981-1) ...

Create and edit the CloudWatch agent configuration file. Run the following command to open the file in Vim editor:

sudo vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

Set the config file code as follows:

{
  "agent": {
    "run_as_user": "cwagent",
    "metrics_collection_interval": 300
  },
  "metrics": {
    "append_dimensions": {
        "InstanceId": "${aws:InstanceId}"
    },
    "metrics_collected": {
      "disk": {
        "measurement": [
          "used_percent"
        ],
        "metrics_collection_interval": 600,
        "resources": [
          "/"
        ]
      }
    }
  }
}

Paste it into VIM editor via following keys:

  • ESC
  • :set paste
  • press i
  • Command + V
  • ESC
  • :wq

Above, disk metrics will be sent to AWS after every 600 seconds (10 minutes).

Restart the agent:

sudo systemctl restart amazon-cloudwatch-agent

Add the service to autostart on boot:

sudo systemctl enable amazon-cloudwatch-agent.service

We ard done withe server part of configuration to send metrices to AWS.

Let’s go to CloudWatch dashboard from AWS console so that we can see where the metrices will be displayed:

Go to All metrics and select CWAgent from the right pane. Select the row and you will see the tpop panel showing the disk usage metrics.