How to use CloudWatch to Monitor RAM of an EC2 instance

In this article, I will discuss how you can use CloudWatch to monitor your AWS EC2 instance’s RAM. CloudWatch is a monitoring tool provided by AWS and it does provide different metrics by default, which means you don’t need to do any customization for these default metrics monitoring. But if you want to monitor RAM then you need to install any additional tool on your EC2 instance which will then show those data in CloudWatch. But before that, let’s discuss what is CloudWatch.

What is CloudWatch

CloudWatch collects monitoring and operational data in the form of logs, metrics, and events, and visualizes it using automated dashboards so you can get a unified view of your AWS resources, applications, and services that run in AWS and on-premises.

So basically CloudWatch is monitoring tool and in next section I will show how you can use cloudwatch to monitor custom metrics as well.

What metrics can be monitored through CloudWatch?

CloudWatch is a monitoring tool from AWS, which monitors different metrics of different AWS services. AWS CloudWatch can collect default metrics from more than 65 AWS services, such as AWS EC2, AWS RDS, AWS DynamoDB, AWS S3, AWS ECS, AWS Lambda, and AWS API Gateway, without taking any custom action. For example, AWS RDS automatically publish CPU utilization, data transfer, and disk usage metrics to CloudWatch. I have provided a list of metrics that can be monitored via CloudWatch.

AWS Service NameMetrics Available in CloudWatch
EC2CPUUtilization, DiskReadOps, CPUCreditUsage, CPUCreditBalance, EBSWriteOps
RDSCPUUtilization, CPUCreditUsage, DatabaseClass, DBClusterIdentifier, EngineName
S3BucketSizeBytes, AllRequests, GetRequests, ReplicationLatency, BytesPendingReplication, OperationsPendingReplication
DynamoDBConditionalCheckFailedRequests, ConsumedReadCapacityUnits, ConsumedWriteCapacityUnits, SystemErrors, ReturnedItemCount

Apart from these built-in metrics, you can monitor custom metrics and log files in CloudWatch. Here is an article has given where I have shown how to monitor apache access log in CloudWatch.

How do I monitor memory on AWS CloudWatch?

To monitor custom metrics from your AWS EC2 instance, you need to install a tool called CloudWatch agent. This tool actually sends data to CloudWatch periodically and from this data, CloudWatch shows us a graph of those data.

To begin the process at first create an EC2 instance in AWS by following my other article here. After creating the instance then login to that instance using Putty or Terminal based on your OS. After that, please set up Apache and PHP so that we can monitor the apache access log. I have an article written on that which you can view here.

So far you have completed the Setup of the Apache 2.4 server, which means we continue further of our tutorial. Next, we want to install cloudwatch Agent, which will send data to CLoudWatch on a periodical interval. This example is developed on Amazon Linux 1 operating system. If you are using a different OS then these commands might not work properly.

At first we need to download the CloudWatch agent RPM file from S3 bucket into our EC2 instance.

cd ~
wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

After that, we are going to install the RPM package which will eventually install our CloudWatch Agent.

sudo rpm -U ./amazon-cloudwatch-agent.rpm

After installing the CloudWatch Agent, we need to create a config file. This config file will tell the agent which metrics to monitor and the time interval. So let’s first open the file using VI for editing.

sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json

Once you open the file “config.json” in edit mode then please paste the content as given below. This is a tested code block that does keep track of the CPU memory_used_percent parameter.

{
    "metrics": {
        "metrics_collected": {
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 30
            } 
        }
    }
}

Once you have pasted that content in the “config.json” file then next, we need to start the CloudWatch Agent by running the command below. This command takes the “config.json” file as a parameter.

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

If you reached so far without any error that means, you have successfully installed the CloudWatch agent on your EC2 instance. Next, wait 5 mins and then go to CloudWatch to view these new matrices.

Conclusion

It’s important to monitor your CPU memory utilization and apache access log. By default, CloudWatch doesn’t monitor these metrics, but using this example now you can easily monitor these two matrices. As a system administrator or DevOps engineer, it’s really important to monitor as many metrics as possible and using this example with the config parameter, you can now monitor any EC2 instance via CLoudWatch.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply