How to set up custom metrics of multiple EC2 instances (Windows Server) with Run Command
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
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
- Click
Next Permissions
. - Attach permissions policies :
AmazonEC2RoleforSSM
- Click
Next Tags
. - Add tags (optional) is skip and click
Next: Reviews
. - Role name* :
custom-metrics-role
- 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
- Select IAM role
custom-metrics-role
- Click
Apply
Step4. Execute Run Command
- In the navigation pane at EC2 console, expand
System Manager Services
, chooseRun Command
, and then chooseRun a command
. - In the
Command document
section, choose a document -AWS-ConfigureCloudWatch
. Select Targets by section, choose Manually Selecting Instances to chose individual instances.
- In the
Status
field, chooseEnable
. Paste the contents of sample json intoProperties
.
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" ] } } }
- If we want to write command output to an Amazon S3 bucket, we select S3 bucket name.
- Choose
Run
and then chooseView results
.
- When the status column shows Success, choose the Output tab.
Step5. Check the Custom Metrics
- Opne the CloudWatch console and check metrics.