9.2 Unified CloudWatch Agent & Custom Metric Collection

Key Takeaways

  • The Unified CloudWatch Agent collects guest OS-level telemetry—including memory utilization, disk space, swap usage, network socket statistics, and per-process metrics (procstat)—which are invisible to the default EC2 hypervisor.
  • The agent is declaratively configured via amazon-cloudwatch-agent.json, divided into agent, metrics (with metrics_collected and procstat), and logs sections (including Windows Event Logs and multi-line application logs).
  • Fleet-wide deployment is managed without SSH/RDP access using AWS Systems Manager (SSM) Run Command or State Manager associations using documents AWS-ConfigureAWSPackage and AmazonCloudWatch-ManageAgent.
  • Agent configuration files should be stored centrally in AWS Systems Manager Parameter Store as String or SecureString parameters, allowing dynamic fleet configuration updates with zero instance downtime.
  • The Unified Agent natively embeds StatsD and collectd daemons, enabling applications to push custom application metrics locally over UDP/TCP port 8125, which the agent aggregates and flushes via PutMetricData.
Last updated: September 2026

Unified CloudWatch Agent Architecture and Capabilities

When Amazon EC2 instances run workloads in AWS, the native CloudWatch hypervisor metrics (AWS/EC2 namespace) provide visibility into the host boundary: CPUUtilization, NetworkIn, NetworkOut, DiskReadOps, and DiskWriteOps (for instance store volumes).

However, the hypervisor cannot inspect guest operating system internals due to virtualization isolation boundaries. The hypervisor has zero visibility into:

  • RAM utilization (active, buffered, cached, free memory)
  • Filesystem disk space utilization and inode consumption
  • Swap file and pagefile utilization
  • Operating system process performance (CPU and memory per process)
  • Local application log files and Windows Event Viewer logs

The Unified CloudWatch Agent is a high-performance Go-based daemon that installs inside the guest OS on both Linux and Windows Server instances, as well as on-premises physical and virtual servers. It replaces legacy, deprecated tools (such as the legacy Python-based awslogsd agent and Perl monitoring scripts) with a single, consolidated telemetry collector.


Declarative Configuration: amazon-cloudwatch-agent.json

The behavior of the Unified CloudWatch Agent is controlled entirely by a JSON configuration file. The configuration is organized into three primary top-level blocks:

  1. agent: Defines global runtime properties (collection intervals, execution user, debug logging).
  2. metrics: Configures OS metric plugins, collection frequencies, custom dimensions, procstat, and embedded telemetry listeners (statsd and collectd).
  3. logs: Configures log file harvesting, Windows Event Log channels, timestamp extraction, and multi-line parsing rules.

Production-Grade Configuration Example

{
  "agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "cwagent"
  },
  "metrics": {
    "namespace": "CWAgent",
    "metrics_collected": {
      "mem": {
        "measurement": [
          "mem_used_percent",
          "mem_available_percent",
          "mem_used",
          "mem_available"
        ],
        "metrics_collection_interval": 60
      },
      "disk": {
        "measurement": [
          "used_percent",
          "inodes_free"
        ],
        "metrics_collection_interval": 60,
        "resources": [
          "/",
          "/var/log",
          "/data"
        ]
      },
      "swap": {
        "measurement": [
          "swap_used_percent"
        ]
      },
      "netstat": {
        "measurement": [
          "tcp_established",
          "tcp_time_wait"
        ],
        "metrics_collection_interval": 60
      },
      "procstat": [
        {
          "pattern": "nginx",
          "measurement": [
            "cpu_usage",
            "memory_rss"
          ]
        },
        {
          "exe": "payment-service",
          "measurement": [
            "cpu_usage",
            "memory_vms"
          ]
        }
      ],
      "statsd": {
        "service_address": ":8125",
        "metrics_collection_interval": 10,
        "metrics_aggregation_interval": 60
      }
    },
    "aggregation_dimensions": [
      ["AutoScalingGroupName"],
      ["InstanceId", "InstanceType"]
    ],
    "append_dimensions": {
      "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
      "ImageId": "${aws:ImageId}",
      "InstanceId": "${aws:InstanceId}",
      "InstanceType": "${aws:InstanceType}"
    }
  },
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "/var/log/application/*.log",
            "log_group_name": "/aws/ec2/production/application",
            "log_stream_name": "{instance_id}",
            "multi_line_start_pattern": "{datetime_format}",
            "timestamp_format": "%Y-%m-%d %H:%M:%S"
          }
        ]
      }
    }
  }
}

Advanced Metric Capabilities: procstat and statsd

  • Process Monitoring with procstat: Monitors individual operating system processes. Processes can be matched using exe (executable name), pattern (command line regex matching), or pid_file (reading process ID from /var/run/*.pid). Metrics emitted include procstat_cpu_usage, procstat_memory_rss (resident set size), and procstat_num_threads.
  • Embedded StatsD Listener: The agent can expose a local UDP or TCP socket (default port 8125). Applications running on the instance can send custom metrics using standard StatsD formatting (statsd.increment("checkout.success")). The agent buffers and aggregates these counters locally before publishing to CloudWatch, drastically reducing PutMetricData API calls.

Fleet-Wide Deployment and Automation with AWS Systems Manager (SSM)

In enterprise DevOps environments, manually connecting to instances via SSH or RDP to install and configure software is strictly prohibited. Fleet-wide agent lifecycle management is orchestrated entirely through AWS Systems Manager.

Centralized Configuration via SSM Parameter Store

Instead of baking configuration files into Amazon Machine Images (AMIs) or hardcoding them on local disks, store the agent configuration centrally in AWS Systems Manager Parameter Store:

  • Parameter Name: /configuration/cloudwatch-agent/linux-production
  • Type: String
  • Value: The raw JSON configuration content.

Step-by-Step Automated Deployment Workflow

1. Attach IAM Instance Profile
   ├── CloudWatchAgentServerPolicy
   └── AmazonSSMManagedInstanceCore
       │
2. Store Agent JSON in SSM Parameter Store
   └── /enterprise/cwagent/linux-config
       │
3. Install Agent via SSM Run Command / State Manager
   └── Document: AWS-ConfigureAWSPackage (Package: AmazonCloudWatchAgent)
       │
4. Configure & Start Agent via SSM Run Command
   └── Document: AmazonCloudWatch-ManageAgent (Action: configure, Mode: ec2)
  1. IAM Permissions: The EC2 instance profile must include:

    • CloudWatchAgentServerPolicy: Grants permissions to publish metrics (cloudwatch:PutMetricData), create log streams, and put log events.
    • AmazonSSMManagedInstanceCore: Grants permissions for the SSM Agent to communicate with Systems Manager.
    • Inline or custom policy allowing ssm:GetParameter on the specific Parameter Store configuration ARN.
  2. Package Installation: Use SSM Run Command or SSM State Manager with document AWS-ConfigureAWSPackage:

    • action: Install
    • name: AmazonCloudWatchAgent
  3. Agent Configuration & Startup: Use SSM document AmazonCloudWatch-ManageAgent:

    • action: configure
    • mode: ec2
    • optional_configuration_source: ssm
    • optional_configuration_location: /configuration/cloudwatch-agent/linux-production
    • optional_restart: yes
  4. Continuous Drift Management with State Manager: Create an SSM State Manager Association targeting instances by tag (e.g., Environment=Production). The association periodically enforces that the agent package is installed and running with the designated Parameter Store configuration, eliminating configuration drift across dynamic Auto Scaling groups.


Windows Telemetry: Performance Counters and Event Logs

On Windows Server instances, the Unified CloudWatch Agent hooks directly into the Windows Management Instrumentation (WMI) subsystem and the Windows Event Log service.

Windows Performance Counters

In the metrics_collected block under windows_events and Windows performance counter sections, administrators collect standard OS counters:

  • Memory: % Committed Bytes In Use, Available MBytes
  • LogicalDisk: % Free Space, Free Megabytes for volumes C:, D:, etc.
  • Paging File: % Usage
  • System: Processor Queue Length

Windows Event Log Ingestion

The logs_collected section captures critical Windows Event channels:

{
  "logs": {
    "logs_collected": {
      "windows_events": {
        "collect_list": [
          {
            "event_name": "System",
            "event_levels": ["ERROR", "CRITICAL"],
            "log_group_name": "/aws/ec2/windows/system",
            "log_stream_name": "{instance_id}"
          },
          {
            "event_name": "Security",
            "event_levels": ["INFORMATION", "WARNING", "ERROR"],
            "log_group_name": "/aws/ec2/windows/security",
            "log_stream_name": "{instance_id}"
          }
        ]
      }
    }
  }
}

Troubleshooting Agent Permissions and Network Connectivity

SymptomRoot CauseRemediation
Agent status is running, but no metrics appear in CWAgent namespaceEC2 IAM role missing cloudwatch:PutMetricData permissionAttach CloudWatchAgentServerPolicy managed policy to instance profile
Instances in private subnets fail to send metrics or logs; timeout errors in agent logPrivate subnet lacks internet egress and has no VPC EndpointsProvision VPC Interface Endpoints for monitoring (com.amazonaws.<region>.monitoring), logs (com.amazonaws.<region>.logs), and ssm (ssm, ssmmessages, ec2messages)
SSM Run Command AmazonCloudWatch-ManageAgent fails with InvalidParameterExceptionParameter Store path is incorrect or IAM role lacks ssm:GetParameter permissionVerify the parameter name matches the SSM document input and verify the instance profile allows ssm:GetParameter on the parameter ARN
Log files stop uploading after log rotationAgent cannot track newly created files because file_path regex does not match the rotated suffixUse wildcards in file_path (e.g., /var/log/app/*.log) and configure multi_line_start_pattern appropriately

Local Verification Commands

On a Linux host, check agent runtime status using the bundled CLI utility:

# Check agent status
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

# Inspect local agent log
sudo tail -n 100 /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log

# Manually apply configuration from local file for testing
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
    -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
Loading diagram...
Unified CloudWatch Agent Fleet Deployment & Telemetry Flow
Test Your Knowledge

A fleet of Amazon EC2 instances hosting an accounting microservice runs in private VPC subnets with no internet gateway, NAT gateway, or public IP addresses. The DevOps team installs the Unified CloudWatch Agent to harvest OS memory utilization and application logs. The EC2 instance profile has the CloudWatchAgentServerPolicy managed policy attached. However, inspecting the local agent log (/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log) reveals continuous dial tcp connection timeout errors when attempting to post telemetry to CloudWatch endpoints, and no metrics appear in the console. How should the DevOps engineer resolve this issue without introducing internet egress routes?

A
B
C
D
Test Your Knowledge

A DevOps engineer needs to deploy and maintain the Unified CloudWatch Agent across a fleet of 500 Amazon EC2 Linux instances launched dynamically by Auto Scaling groups. The configuration requires monitoring memory utilization, disk space, and application log files. The solution must support updating the configuration across all instances centrally without rebuilding AMIs, without logging into instances directly, and ensuring newly launched instances automatically receive the latest configuration. Which design achieves these requirements?

A
B
C
D
Test Your Knowledge

A DevOps team manages a containerized payment processing service running on Amazon EC2. The team observes periodic memory exhaustion issues caused by memory leaks in the payment worker process, as well as unpredictable spikes in rejected transactions. The team needs to: (1) track the resident memory footprint of the specific binary named payment-worker, and (2) allow the application code to push custom transaction rejection counters locally without issuing high-frequency PutMetricData API calls. How should the amazon-cloudwatch-agent.json configuration file be structured to fulfill these requirements?

A
B
C
D