Monitoring EC2 Memory with CloudWatch

An important AWS exam topic is to know what Cloudwatch can monitor by default. If you go to the cloudwatch metrics, you can see some of the default metrics, as seen below:

We have metrics like CPU Utilization, NetworkIn and Out, DiskRead, Disk Write and others.

There is no default metric for memory however. And that is a question that I’ve seen pop up on AWS Exams.

But there is a way to view the memory usage of your EC2 instances. It takes work and using some scripts however to do it right. We will learn how to do this using the AWS documentation and a little help from acloud.guru. Below is the link to the AWS documentation.

To begin, we launch an Amazon Linux EC2 instance and give it an IAM role with full access to Cloudwatch. You can create the role in the EC2 screen or in IAM first. If you don’t add the role to the EC2, then it won’t be able to communicate to CloudWatch to report the metrics, so this is a key step.

After you do this, then SSH into the EC2 instance. The command is straightforward:

ssh ec2-user@public-ip -i keypair.pem

remember to be in the correct directory of the keypair and to make sure your keypair has the 400 permission so it can connect.

After you connect, do a “sudo su” to become the root user and do a yum update -y to update the OS.

Now it’s time to install the custom perl packages that will run the perl scripts that send data back to CloudWatch.

Then we “Run the following commands to install the monitoring scripts:”

unzip CloudWatchMonitoringScripts-1.2.2.zip && \
rm CloudWatchMonitoringScripts-1.2.2.zip && \
cd aws-scripts-mon
Once we install the monitoring scripts, then we can test the connection from our EC2 instance to Cloudwatch.

./mon-put-instance-data.pl --mem-util --verify --verbose
If everything is correct, you should receive a verification successful message:

Now that we know the connection from EC2 to Cloudwatch is active, we finally send the Memory usage metrics to cloudwatch. Type the following command:
./mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-avail

This command reports Memory Utilized, Memory Used, and Memory Available. After you run this command, you should get the success message.

Now you can go to the Cloudwatch console and see the metrics directly.