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.
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.
For permissions, we’ll use the default AWSEC2RoleforSSM.
Then, we’ll give the Policy a name and select Create Role.
Now, it is necessary to apply this role to the AWS instance.
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.
Select Run a command…
Select Command document AWS-ConfigureCloudWatch
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.
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.
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.
CloudWatch is plotting that we have ~55% free space on C:\ for our instance.
As a reality check, we’ll log into the instance and check- this checks out!
From CloudWatch, we could easily setup an alarm to send an email when Disk Space is less that 5%.
One thought on “Monitoring Free Disk Space on a Windows EC2 Instance with CloudWatch”