How to set up custom metrics of multiple EC2 instances (Windows Server) with Run Command

2019.03.09

In this post, we will introduce how to setup custom metrics of disk usage and memory usage on multiple Windows Server using Run Command.

Please see blog below for how to setup Amazon Linux.

How to set up custom metrics of multiple EC2 instances (Amazon Linux) with Run Command

Environment

  • Windows 2016 AMI (Windows_Server-2016-English-Full-Base-2019.02.13 (ami-0af80d239cc063c12))

How to Setup

Step1. Create Tags for EC2 instances

When you want to manage with tags, we create Tags for EC2 instances. This example adds the tag custom-metrics=enable

image

Step2. Create IAM role

From the IAM management console, create IAM role.

We will create an IAM role that trusts EC2 service and attache AmazonEC2RoleforSSM SSM policy.

  • Roles → Create role
  • Select type of trusted entity : AWS service
  • Choose the service that will use this role : EC2

image

  • Click Next Permissions.
  • Attach permissions policies : AmazonEC2RoleforSSM

image

  • Click Next Tags.
  • Add tags (optional) is skip and click Next: Reviews.
  • Role name* : custom-metrics-role

image

  • Click Create role.

Step3. Attach IAM Role for EC2 instances

From the EC2 management console, attach the IAM role custom-metrics-role to the EC2 instance.

  • Select your EC2 instance → Actions → Instance Settings → Attach/Replace IAM Role

image

  • Select IAM role custom-metrics-role

image

  • Click Apply

Step4. Execute Run Command

  • In the navigation pane at EC2 console, expand System Manager Services, choose Run Command, and then choose Run a command.
  • In the Command document section, choose a document - AWS-ConfigureCloudWatch. Select Targets by section, choose Manually Selecting Instances to chose individual instances.

image

  • In the Status field, choose Enable. Paste the contents of sample json into Properties.

sample json

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
{
"Id": "MemoryAvailableMegabytes",
"FullName": "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"CategoryName": "Memory",
"CounterName": "Available MBytes",
"InstanceName": "",
"MetricName": "MemoryAvailable",
"Unit": "Megabytes",
"DimensionName": "InstanceId",
"DimensionValue": "{instance_id}"
}
},
{
"Id": "MonitoringFreeSpaceC",
"FullName": "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"CategoryName": "LogicalDisk",
"CounterName": "% Free Space",
"InstanceName": "C:",
"MetricName": "DiskFreeC",
"Unit": "Percent",
"DimensionName": "InstanceId",
"DimensionValue": "{instance_id}"
}
},
{
"Id": "CloudWatchLogs",
"FullName": "AWS.EC2.Windows.CloudWatch.CloudWatchLogsOutput,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"AccessKey": "",
"SecretKey": "",
"Region": "us-west-2",
"LogGroup": "Default-Log-Group",
"LogStream": "{instance_id}"
}
},
{
"Id": "CloudWatch",
"FullName": "AWS.EC2.Windows.CloudWatch.CloudWatch.CloudWatchOutputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters":
{
"AccessKey": "",
"SecretKey": "",
"Region": "us-west-2",
"NameSpace": "Classmethod/EC2"
}
}
],
"Flows": {
"Flows":
[
"(MonitoringFreeSpaceC,MemoryAvailableMegabytes),CloudWatch"
]
}
}
}

image

  • If we want to write command output to an Amazon S3 bucket, we select S3 bucket name.
  • Choose Run and then choose View results.

image

  • When the status column shows Success, choose the Output tab.

image

Step5. Check the Custom Metrics

  • Opne the CloudWatch console and check metrics.

image image