How to Create EC2 Custom Metrics with Amazon CloudWatch



When you want to monitor the performance and reliability of an EC2 instance in AWS, Amazon CloudWatch probably comes to mind first. This integrated AWS monitoring and observability service helps IT teams track performance and collect data from a variety of AWS cloud services and resources. For EC2 instances, CloudWatch provides typical monitoring of CPU usage, hard drive usage, network ingress, and network egress.

But what if you want to take surveillance a step further? This is where EC2 custom metrics come in.

Why Create Custom Metrics in CloudWatch?

CloudWatch is a powerful tool for monitoring cloud applications, but built-in monitoring cannot do it all. For example, it does not monitor traffic on port 80 or port 443. It also does not monitor if your Nginx server is functioning as expected.

A custom metric allows you to monitor a specific application binary or runtime. CloudWatch helps you monitor the infrastructure part of an EC2 instance, such as processor, hard drive, and network. However, if the application running on the instance is stopped or in a warning state, standard CloudWatch monitoring will not provide much information.

When CloudWatch doesn’t have a metric for your specific use case, you’ll want to implement a custom metric.

Let’s see what it takes to create a custom metric in CloudWatch. In the following step-by-step tutorial, we walk through the steps to have CloudWatch monitor a web server running on a Linux EC2 instance to confirm that port 443 is open for inbound HTTPS traffic. Before you begin, make sure that you have an EC2 instance running Linux with a role association to access CloudWatch. You also need EC2 SSH access to the instance.

Create a custom metric

To create a custom metric in CloudWatch, you use either AWS Management Console or a scenario. IT teams should try to automate as much as possible in any environment, and a script is a great method of automation. The automation script in this example is native to Linux and featured in Bash, but you can use any programming language for this purpose.

Create the automation script. To get started, connect in SSH to the EC2 instance. Create a new file called for the automation code. Open the file with Vim or Nano, and copy / paste the following code:

PORT_443=$(netstat -an | grep 443 | wc -l)
aws cloudwatch put-metric-data --metric-name PORT_443_AVAILABILITY --dimensions Instance=i-0255e296e993b6df1 --namespace "port443" --value $

The automation code creates a variable that uses the netstat order to grep for port 443 to make sure it is working. Use AWS Command Line Interface with the cloud watch to create the custom metric using the put-metric-data option. The put-metric-data The option contains four parameters: the name of the metric, the instance ID you want to monitor, the namespace, and the value of the metric, which is the grepping variable for port 443.

The last step is to give the appropriate permissions to the system to run the script. Use the following chmod order:

chmod +x

Create the cron job. Once the script is saved, it’s time to run it. Make sure the script is running continuously so that custom metrics are always updated in the CloudWatch console.

To create a cron job, run the following command on the EC2 instance:

crontab -e

The cron job will open and you are now ready to configure the cron. To ensure that the CloudWatch custom metric extracts data from the EC2 instance in a reasonable manner, configure the cron job to run the script continuously.

*/1 * * * * /home/ec2-user/

Once you have registered the cron job, you will see terminal output indicating that the cron is running.

Cron Job Code Metrics

Check the custom metric in CloudWatch. For the last step, open the AWS Management Console and go to the CloudWatch service to verify the metric. Under the CloudWatch service, click Metrics.

AWS Management Console CloudWatch Service Screens

Under All metrics, there is a new section for Custom metric.

Custom metrics

Click on Custom, and the new custom metric is now available.

CloudWatch custom metric available

Custom metrics with other cloud providers

There are plenty of options for creating custom metrics for those using another cloud provider or third-party service.

In Google Cloud Platform (GCP), for example, you can create custom metrics with the OpenCensus service, which is a collection of libraries for several programming languages ​​that allow you to collect application metrics. You can also use the GCP Cloud Monitoring API to create custom metrics, which work with C #, Go, Java, Node.js, PHP, Python, and Ruby.

There are also many monitoring tools available for Azure cloud users. Microsoft put custom metrics into preview mode in 2021 for Azure Monitor. The preview allows you to send metrics to Azure in several ways:

  • Azure Application Insights SDK;
  • Azure Monitor agent on Linux or Windows virtual machines;
  • Windows Azure diagnostic extension;
  • Agent InfluxData Telegraf; and
  • custom metrics through the Azure Monitor API.



Leave A Reply