How to Enable EC2 Memory Metrics in CloudWatch

A simple guide to getting RAM usage data into CloudWatch so you can actually perform EC2 rightsizing without flying blind.

AWSComputeEC2
How to Enable EC2 Memory Metrics in CloudWatch

Introduction

If you want to reduce EC2 costs, you need to know how much memory your instances are actually using. By default, CloudWatch does not show you RAM usage. This makes EC2 rightsizing difficult because you can't be sure if a smaller instance will run out of memory.

To get this data, you have to install the CloudWatch Agent on your instances. It is a one time setup that gives you the visibility needed for proper AWS rightsizing.

1. Create an IAM Role

Your EC2 instances need permission to talk to CloudWatch. Go to the IAM console and create a new role for the EC2 service.

Search for the policy named CloudWatchAgentServerPolicy and attach it to the role. Give the role a name like CloudWatchAgentRole and save it. Then, go to the EC2 console, select your instance, and under Actions > Security, attach this new role.

2. Install the Agent

Most Amazon Linux and Ubuntu instances can pull the agent directly from the standard repositories. Connect to your instance and run the install command.

For Amazon Linux:

sudo yum install amazon-cloudwatch-agent

For Ubuntu:

sudo apt-get install amazon-cloudwatch-agent

3. Run the Configuration Wizard

AWS includes a wizard that walks you through creating the configuration file. It is much easier than writing the JSON by hand. Start it with this command:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

The wizard will ask several questions. When it asks about the level of monitoring, choose "Standard" or "Advanced" to make sure memory metrics are included. You can skip the logs section if you only need the metrics for an AWS cost optimization tool.

4. Start the Agent

The wizard saves a configuration file, but it doesn't start the agent automatically. You need to trigger the agent to fetch that file and start running:

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

The agent sends memory metrics to CloudWatch at one minute intervals by default. You can adjust this in the configuration file if needed.

5. View Your Metrics

Head over to the CloudWatch console and look under Metrics > All metrics. Look for mem_used_percent in the CWAgent namespace. Track it for a few weeks to see actual memory usage before changing instance types.

Conclusion

Setting up memory monitoring takes just a few minutes. Once running, you’ll have reliable data to make informed EC2 rightsizing decisions.

Automate Your EC2 Rightsizing

Infralyst continuously collects memory usage from all your instances. When it spots savings, you’re one click away from a ready-to-merge Terraform PR.

Start free with 3 PRs

No credit card required · Read-only IAM role · Your team reviews and merges every change