Lesson 4 of 5 CloudWatch Logs and the CloudWatch Agent

Installing and Configuring the CloudWatch Agent

This lesson explains the unified CloudWatch agent, the standard answer whenever a question needs memory, disk or process metrics, or application log files, from EC2 instances and on-premises servers.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify what the agent collects.
  • Grant permissions and distribute configuration through Parameter Store.
  • Troubleshoot missing agent metrics and logs.

What the agent collects

The agent collects metrics that the hypervisor cannot see, such as mem_used_percent, swap_used_percent, disk_used_percent per mount point, disk I/O, network statistics, TCP connection counts and, with procstat, per-process CPU and memory. Metrics go to the CWAgent namespace by default. It also tails log files and Windows event logs and sends them to CloudWatch Logs, and it can receive StatsD and collectd metrics and OpenTelemetry or X-Ray traces from applications.

Permissions, configuration and installation

The agent needs three things. First, credentials: on EC2 an instance profile role with the CloudWatchAgentServerPolicy managed policy (add AmazonSSMManagedInstanceCore to manage it with Systems Manager); on premises an IAM user profile or IAM Roles Anywhere. Second, a configuration file in JSON with agent, metrics and logs sections, created with the configuration wizard or by hand and usually stored in Parameter Store with a name starting with AmazonCloudWatch-. Third, network access to the CloudWatch (monitoring) and CloudWatch Logs (logs) endpoints through an internet gateway, NAT gateway or interface VPC endpoints.

For a fleet, install the agent with Systems Manager Run Command (AWS-ConfigureAWSPackage with the package name AmazonCloudWatchAgent) and apply the configuration with AmazonCloudWatch-ManageAgent, which reads the configuration from Parameter Store. A State Manager association repeats this on a schedule so that new instances, including those launched by Auto Scaling, are configured automatically. The configuration can add append_dimensions such as AutoScalingGroupName and InstanceId, and aggregation_dimensions to publish group-level metrics.

{
  "agent": {"metrics_collection_interval": 60},
  "metrics": {
    "namespace": "CWAgent",
    "append_dimensions": {"AutoScalingGroupName": "${aws:AutoScalingGroupName}", "InstanceId": "${aws:InstanceId}"},
    "aggregation_dimensions": [["AutoScalingGroupName"]],
    "metrics_collected": {
      "mem": {"measurement": ["mem_used_percent"]},
      "disk": {"measurement": ["used_percent"], "resources": ["/"]}
    }
  },
  "logs": {"logs_collected": {"files": {"collect_list": [
    {"file_path": "/var/log/app/app.log", "log_group_name": "/app/prod", "log_stream_name": "{instance_id}",
     "retention_in_days": 90}
  ]}}}
}

The following diagram shows the three requirements around an instance.

The three things the CloudWatch agent needs. Credentials, from an instance profile with CloudWatchAgentServerPolicy. A JSON configuration, usually stored in Parameter Store under a name starting with AmazonCloudWatch-. And network access to the CloudWatch and CloudWatch Logs endpoints through a NAT gateway or interface endpoints. Systems Manager installs and configures it across a fleet.
The parameter name is illustrative. A missing piece shows as an agent that runs but publishes nothing.
Put in order

Deploying the agent to a fleet

Exam skills 1.1.2 · 3.2.1

Put the steps for rolling out the CloudWatch agent with Systems Manager in order.

Log in to try this activity Interactive activities are free with an account.

Troubleshooting the agent

When agent metrics or logs do not appear, work through the common causes in order. The following list gives the checks and where to look.

  1. Is the agent running?

    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a status.

  2. Did it load the configuration?

    Check /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log for parse errors or wrong file paths.

  3. Does the role allow it?

    The instance role needs CloudWatchAgentServerPolicy; AccessDenied errors appear in the agent log.

  4. Can it reach the endpoints?

    Private subnets need a NAT gateway or interface endpoints for monitoring and logs.

  5. Are you looking in the right place?

    Metrics are in the CWAgent namespace with the appended dimensions, not in AWS/EC2.

Troubleshooting scenario

Where are the metrics?

Exam skills 1.1.2

Diagnose why agent metrics or logs are missing.

Log in to try this activity Interactive activities are free with an account.
Worked example

Memory metrics for an Auto Scaling group

A team needs to scale a group on memory. They store the configuration above in Parameter Store as AmazonCloudWatch-linux-web, attach CloudWatchAgentServerPolicy and AmazonSSMManagedInstanceCore to the instance profile in the launch template, and create a State Manager association that runs AWS-ConfigureAWSPackage and then AmazonCloudWatch-ManageAgent on every instance tagged Role=web every 30 minutes. Because the configuration aggregates on AutoScalingGroupName, CloudWatch receives a group-level mem_used_percent metric, and a target tracking policy uses it with a target value of 70.

Check yourself

Where should the agent configuration be stored for a fleet of instances?

Show answer

In Parameter Store, from which Systems Manager deploys it to every instance.

Exam Tips

Remember for the exam

  • Memory, disk and process metrics need the CloudWatch agent; they appear in the CWAgent namespace.
  • The instance role needs CloudWatchAgentServerPolicy.
  • Store agent configuration in Parameter Store and deploy it with Systems Manager.
  • Private subnets need NAT or interface endpoints for monitoring and logs.
  • Use aggregation_dimensions to publish Auto Scaling group-level agent metrics.