Monitoring AWS EC2 Windows instances using AWS Systems Manager and CloudWatch agent

Reo Togashi
Gecogeco
Published in
7 min readFeb 21, 2023

--

Overview

Sometimes we want to monitor the free space of AWS EC2. To do so, we can manually install CloudWatch agent to the instance and set it up, and start the CloudWatch agent. However, it would be a hassle if we have multiple instances to do the same setting. This article introduces how to introduce monitoring setup for AWS EC2 Windows instances using AWS System Manager.

This article mainly consists of the following parts.

* Introduction
* Verification
* Summary
* Reference

Introduction

This section consists of the following parts.

* What do I want to do in this article?
* What is the motivation?
* How can we monitor free space of Windows instance?
* What is AWS Systems Manager?
* What is an advantage of AWS SSM?
* What is a goal of this article?

What do I want to do in this article?

I want to know how much space is available on each drive on the Windows server

What is the motivation?

  • Windows server is used in one of my projects
  • Application logs accumulate regularly in the server. Therefore, the free space will eventually run out and the application may not work in the future.
  • In order to avoid the situation, I would like to monitor how much space is available on each drive on the Windows server to detect in advance.

How can we monitor free space of Windows instance?

What is AWS Systems Manager?

AWS official document is referenced to explain about it.

AWS Systems Manager is the operations hub for your AWS applications and resources and a secure end-to-end management solution for hybrid cloud environments that enables secure operations at scale.

Ref: https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html

Though there are several features in AWS SSM, Parameter Store and Run Command are used in this article.

Parameter Store

You can store data such as passwords, database strings, Amazon Machine Image (AMI) IDs, and license codes as parameter values. You can store values as plain text or encrypted data. You can reference Systems Manager parameters in your scripts, commands, SSM documents, and configuration and automation workflows by using the unique name that you specified when you created the parameter.

Ref: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html

Run Command

Run Command allows you to automate common administrative tasks and perform one-time configuration changes at scale.

Ref: https://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html

What is an advantage of AWS SSM?

You can apply the same command to multiple instances at once.

In my case, I have several instances and need to apply the same setting like the following.

  • Install CloudWatch Agent
  • Based on the specific setting, start CloudWatch Agent.

In the case that you have a single instance, SSM may not need to be used. However, when working on multiple instances, it may be better to use SSM in order to reduce your effort.

What is a goal of this article?

Send free space metric of target Windows server to AWS CloudWatch. Here is an overview of what we are going to create in order to achieve the goal.

Figure 1: AWS Overview
  • We will prepare above AWS environment first.
  • Run SSM Run Command to install and start CloudWatch Agent
  • Confirm that the free space metic is sent to CloudWatch.

Verification

This section consists of the following parts.

* Preparation: AWS environment by AWS CDK
* Preparation: Run SSM Run Command to install and start CloudWatch Agent
* Result: Is the free space value correct ?
* Result: Could we get free space metric from Windows Server?

Preparation: AWS environment by AWS CDK

For this, AWS CDK is used to generate most of the resources in the above figure. The CDK is already prepared in the following repository and you can use it.
Repository: https://github.com/reotogashi/ssm-cloudwatchagent-demo

The following resources are created by this AWS CDK.

Preparation: Run SSM Run Command to install and start CloudWatch Agent

How to install CloudWatch Agent

Open the Systems Manager console at https://console.aws.amazon.com/systems-manager/.

In the navigation pane, choose Run Command.

Set as follows and choose Run.

* Command document: AWS-ConfigureAWSPackage
* Command parameters - Action: Install
* Command parameters - Name: AmazonCloudWatchAgent
* Command parameters - Version: latest
* Target selection - Target selection: Choose instances manually
* Target selection - Name: {Your instance id}
* Output options - S3 Bucket: Disable
* Output options - CloudWatch log: Disable
Figure 2: Install CloudWatch Agent by AWS SSM Run command

How to start CloudWatch Agent using AWS SSM Paramter store

Open the Systems Manager console at https://console.aws.amazon.com/systems-manager/.

In the navigation pane, choose Run Command.

Set as follows and choose Run.

* Command document: AmazonCloudWatch-ManageAgent
* Command parameters - Action: configure
* Command parameters - Mode: ec2
* Command parameters - Source: ssm
* Command parameters - Optional Configuration: AmazonCloudWatch-test
* Optional Restart: yes
* Target selection - Target selection: Choose instances manually
* Target selection - Name: {Your instance id}
* Output options - S3 Bucket: Disable
* Output options - CloudWatch log: Disable
Figure 3: Start CloudWatch Agent by AWS SSM Run command

AmazonCloudWatch-test above is a Parameter store created by CDK. By checking the detail, you can see the following content, which needs to tell CloudWatch agent to get the free space of Drive from Windows server.

{
"metrics": {
"aggregation_dimensions": [
[
"InstanceId"
]
],
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"LogicalDisk": {
"measurement": [
"% Free Space"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
}
}
}
}

Result: Could we get free space metric from Windows Server?

Yes. When you check the CloudWatch with the following steps, you can see the graph showing the percentage of free space of C drive in the target instance.

  • Go to CloudWatch console
    * Click All metrics in Metrics in left panel
    * Click CWAgent in Custom namespaces
    * Click ImageId, InstanceId, InstanceType, instance, objectname in Metrics
  • Check the checkbox whose …
    * Instance name is your instance name
    * instance is C:
    * objectname is LogicalDesk

As you can see the graph, the free space is about 51.6%.

Figure 4: AWS CloudWatch Metric

Result: Is the free space value correct?

Yes. Let us check by accessing to Windows server. To do so, let us run some AWS CLI commands.

In order to get the Windows login password, let us get pem file with the following command. the file is stored in AWS Secrets Manager when created by AWS CDK.

aws secretsmanager get-secret-value \
--secret-id ec2-ssh-key/test-ec2-keypair/private \
--query SecretString \
--output text \
--profile {your profile} > cdk-key.pem && chmod 400 cdk-key.pem

Next, let us confirm the instance id we have created by AWS CDK.

aws ec2 describe-instances \
--filter "Name=key-name,Values=test-ec2-keypair" \
--profile {your profile}

By using per file and instance id, let us get the Windows login password as follows.

$ aws ec2 get-password-data \
--instance-id i-XXXXXXXXXXXXXXXXXX \
--priv-launch-key cdk-key.pem \
--profile {your profile}

// Sample output below
{
"InstanceId": "i-XXXXXXXXXXXXXXXXXX",
"PasswordData": "hogefuga",
"Timestamp": "2023-02-17T04:07:49+00:00"
}

After that, let us start a session to access to Windows Server by AWS SSM.

aws ssm start-session \
--target {your instance id} \
--document-name AWS-StartPortForwardingSession \
--parameters portNumber=3389,localPortNumber={your preferable port} \
--profile {your profile}

// Sample output below
Starting session with SessionId: xxx-xxx-xxxxxxxxxxxxxxxxx
Port 33389 opened for sessionId xxx-xxx-xxxxxxxxxxxxxxxxx.
Waiting for connections...

In my case, I set localPortNumber=33389.

After that, you can open the Remote Desktop application and enter localhost:33389 and Connect.

Figure 5: Remote desktop application

After you input the Windows login password obtained by the above steps.

Figure 6: Input Windows Login Password

You can log in to your Windows Server now. Then you can check the free space information by Performance Monitor as follows.

Figure 7: Performance Monitor in Windows Server

As you can see, the value is about 51.6, which is basically the same as the one in CloudWatch in Figure 1.

Summary

In this article, how to install and start CloudWatch by AWS Systems Managers for AWS EC2 Windows instances is introduced for monitoring of free space. The verification result shows that the free space value is sent to AWS CloudWatch and the value is also verified by accessing to Windows Server. If you need to have the same setup for multiple instances, this approach would be helpful. Thank you for reading 🙌

Reference

AWS resources created by AWS CDK

Reference related to AWS SSM

--

--