Guided Lab: Installing and configuring CloudWatch Agent
Description
CloudWatch Agent is a software package that you can install on your EC2 instances or on-premises servers for monitoring. It collects detailed system-level metrics such as CPU, memory, disk, and network usage. The agent also lets us publish custom metrics to monitor specific application needs, like tracking unique business or operational Key Performance Indicators (KPIs).
Prerequisites
This lab assumes you have experience creating EC2 instances and are familiar with its basic components.
If you find any gaps in your knowledge, consider taking the following labs:
- Creating an Amazon EC2 instance (Linux)
- Setting up a Web server on an EC2 instance
Objectives
In this lab, we’ll set up an EC2 instance to demonstrate how to install and configure CloudWatch Agent.
Lab Steps
Connecting to your EC2 Instance via EC2 Instance Connect
1. Search “ec2” in the AWS Management Console search bar. Click EC2 on the search results.

2. Launch EC2 instance with the following configurations.
- Name: CWAgent
- AMI: Ubuntu
- Instance type: t2.micro
- Key pair: If you don’t already have a key pair, please create a new one.
- Key pair name: CWAgent-ssh
- Key pair type: RSA
- Private key file format: pem
- Network settings:
- Allow SSH traffic from: My IP
3. Create an IAM Role: Before CloudWatch will work with an EC2 instance, it must have an IAM role. For this lab, we will be using the provided IAM role.
4. Before you proceed, make sure that the Instance state is Running.
5. Attach the IAM Role to the EC2 instance: Navigate to the EC2 console and select your instance. Click on Actions -> Security -> Modify IAM role.

6. Choose PlayCloud-Sandbox.

7. Click Update IAM role.
Installing the CloudWatch Agent
1. Download the CloudWatch Agent Package: SSH into your instance and run the command below:
sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
This command simply downloads the agent from an S3 bucket to the current directory.

2. Install the CloudWatch Agent: Run the command below:
sudo dpkg -i ./amazon-cloudwatch-agent.deb
This command installs the agent using the DEB package manager.

Configuring the CloudWatch agent to send custom metrics to CloudWatch
1. Edit the agent’s configuration file. Run sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json
- Copy-paste the following:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"append_dimensions": {
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"/"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
}
}
}
}
- Enter
ESC :wq!
to save the file.
2. Stop and Start the CloudWatch Agent:
- Stop the CloudWatch Agent using the command line.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop
- Start the CloudWatch agent using the command line.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
3. Validate Agent is Active: Start the agent, check the agent logs, and review the console for server log data and metrics. Run the following commands.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

cat /opt/aws/amazon-cloudwatch-agent/logs/configuration-validation.log

4. Confirm if the custom metrics are being sent to CloudWatch.
- Go to CloudWatch → Metrics → All Metrics → CWAgent

5. Now, let’s break down each metric in your configuration:
metrics_collection_interval
: This is the frequency at which the metrics are collected. It’s set to 60 seconds, which means the CloudWatch agent will collect metrics every minute.run_as_user
: This is the user that the CloudWatch agent runs as. It’s set to “root”, which means the agent has full permissions on the system.append_dimensions
: These are the default dimensions that are assigned to all collected metrics. They include:ImageId
: The ID of the Amazon Machine Image (AMI) used to launch the instance.InstanceId
: The ID of the instance.InstanceType
: The type of the instance.

metrics_collected
: This is where you specify the metrics that you want to collect. You’re collecting disk and memory metrics:disk
: This collects metrics about disk usage. The metrics include:used_percent
: The percentage of disk space used.free
: The amount of free disk space.total
: The total amount of disk space.metrics_collection_interval
: The frequency at which the disk metrics are collected. It’s set to 60 seconds.resources
: The file paths that the disk metrics are collected from. It’s set to “/”, which represents the root directory.

mem
: This collects metrics about memory usage. The metrics include:mem_used_percent
: The percentage of memory used.cached
: The amount of cached memory.total
: The total amount of memory.free
: The amount of free memory.metrics_collection_interval
: The frequency at which the memory metrics are collected. It’s set to 60 seconds.

Remember, you can customize this configuration to fit your specific needs by adding or removing metrics, changing the collection intervals, or modifying the dimensions.