How can I send memory and disk metrics from my EC2 instances to CloudWatch?

3 minute read
1

I want to send memory and disk metrics from my Amazon Elastic Compute Cloud (Amazon EC2) instances to Amazon CloudWatch Metrics. How can I do this?

Short description

By default, Amazon EC2 delivers a set of metrics related to your instance to CloudWatch in the AWS/EC2 namespace. This includes CPU utilization and a set of NetworkIn and NetworkOut metrics.
Note: EC2 doesn't provide metrics related to OS-level memory usage or disk usage metrics.

To find these metrics and deliver them to CloudWatch as custom metrics, install the unified CloudWatch Agent. Then, define these metrics in the Agent configuration file.

Important: Custom metrics are charged according to their storage and API use.

Resolution

Note: If you receive errors when you run the AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

You can download and install the CloudWatch agent manually using the AWS CLI, or you can integrate it with AWS Systems Manager Agent (SSM Agent).
Note: The CloudWatch agent is supported on both Windows and Linux systems.

Use these steps to install the CloudWatch agent:

  1. Create IAM roles or users that activate the agent that collects metrics from the server. Optionally, integrate with AWS Systems Manager. Attach this IAM role to the EC2 instance that you want to install the agent on.

  2. Download the agent package, and then install the agent package.

  3. Create the CloudWatch agent configuration file, and then specify the metrics that you want to collect.

    This code example shows a basic agent configuration file that reports memory usage and disk usage metrics on a Linux system:

    {
      "metrics": {
        "metrics_collected": {
          "mem": {
            "measurement": [
              "mem_used_percent"
            ]
          },
          "disk": {
            "measurement": [
              "used_percent"
            ],
            "resources": [
              "*"
            ]
          }
        },
        "append_dimensions": {
          "InstanceId": "${aws:InstanceId}"
        }
      }
    }

    This code example shows a basic agent configuration file for Windows systems:

    {
      "metrics": {
        "metrics_collected": {
          "LogicalDisk": {
            "measurement": [
              "% Free Space"
            ],
            "resources": [
              "*"
            ]
          },
          "Memory": {
            "measurement": [
              "% Committed Bytes In Use"
            ]
          }
        },
        "append_dimensions": {
          "InstanceId": "${aws:InstanceId}"
        }
      }
    }
  4. Start the agent on your EC2 instance.

By default, the active agent reports metrics from your instance to the CWAgent namespace within CloudWatch. If you experience issues, then see Troubleshooting the CloudWatch agent.

Related information

Monitor your instances using CloudWatch

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago