Monitoring Free Disk Space on a Windows EC2 Instance with CloudWatch

By default, CloudWatch has some native monitoring for EC2 instances (CPUUtilization, NetworkIn/Out, DiskWriteBytes/DiskReadBytes, etc).  However, a few key parameters are missing by default- one of which is free disk space.

Free disk space requires a custom CloudWatch monitor.  Let’s go through how to set that up.

In the example below, we have a Server 2016 instance running in US-East-1B.

cw1.png

Server 2016 uses the SSM Agent to relay info to CloudWatch.  The SSM Agent is already installed in the AWS Windows Server 2016 base AMI.

Now, our EC2 instance needs permissions to PUT data to CloudWatch.  We’ll do this by creating an IAM Role and then assigning it to the instance.

If you’re not familiar, roles are a convenient way to give permissions without having to store the keypair within an instance for access to AWS resources.

Go to IAM > Roles > Create Role. Then we’ll specify EC2 as the service that will use the role as shown below.

cw2.png

For permissions, we’ll use the default AWSEC2RoleforSSM.

cw3_001.png

Then, we’ll give the Policy a name and select Create Role.

cw4.png

Now, it is necessary to apply this role to the AWS instance.

cw5_001.png

cw7.png

Ok, now our instance will be able to write logs to CloudWatch.  Ok, let’s get in to how we tell the SSM Agent to PUT the data to CloudWatch…

Within the EC2 Console, go to Run Command.

cw8.png

Select Run a command…

cw9.png

Select Command document AWS-ConfigureCloudWatch

cw10.png

We’ll specify targets…I’ll select our test instance here, but in production, it’s more practical to specify a Tag. As I’m only executing this on test 1 instance, there isn’t much concern for performance hits from simultaneous executions, but typically, there would be consideration as to how aggressively the given command is pushed out to a fleet.

cw11.png

With the status enabled, we’ll need to specify the JSON document for our desired action in Properties.

We’re wanting to monitor for free disk space on our C:\ drive.

cw12.png

Here’s the JSON for that:

{
    "IsEnabled": true,
    "EngineConfiguration": {
        "PollInterval": "00:00:15",
        "Components": [
            {
                "Id": "PerformanceCounterDisk",
                "FullName": "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch",
                "Parameters": {
                "CategoryName": "LogicalDisk",
                "CounterName": "% Free Space",
                "InstanceName": "C:",
                "MetricName": "C.FreeDiskPercent",
                "Unit": "Percent",
                "DimensionName": "Instance",
                "DimensionValue": "Hostname: {hostname} IP Address: {ip_address} InstanceId: {instance_id}"
                }
           },
           {
                "Id": "CloudWatch",
                "FullName": "AWS.EC2.Windows.CloudWatch.CloudWatch.CloudWatchOutputComponent,AWS.EC2.Windows.CloudWatch",
                "Parameters":
                {
                    "AccessKey": "",
                    "SecretKey": "",
                    "Region": "us-east-1",
                    "NameSpace": "Windows/Default"
                }
           }
       ],
       "Flows": {
           "Flows":
           [
               "PerformanceCounterDisk,CloudWatch"
           ]
       }
    }
}

 

Once the command says Success we’ll check CloudWatch.cw13.png

CloudWatch is plotting that we have ~55% free space on C:\ for our instance.

cw14.png

As a reality check, we’ll log into the instance and check- this checks out!

cw15.png

From CloudWatch, we could easily setup an alarm to send an email when Disk Space is less that 5%.

cw17.png

 

 

 

 

 

 

 

One thought on “Monitoring Free Disk Space on a Windows EC2 Instance with CloudWatch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s