How you can create EC2 custom metrics with Amazon CloudWatch in 2023

This tutorial is about How you can create EC2 custom metrics with Amazon CloudWatch. Recently I updated this tutorial and will try my best so that you understand this guide. I hope you guys like this blog, How you can create EC2 custom metrics with Amazon CloudWatch. If your answer is yes after reading the article, please share this article with your friends and family to support us.

Check How you can create EC2 custom metrics with Amazon CloudWatch

When you want to monitor the performance and reliability of an EC2 instance on AWS, Amazon CloudWatch will likely come to mind first. This integrated monitoring and observability service from AWS helps IT teams track performance and collect data from many resources and services in the AWS cloud. For EC2 instances, CloudWatch provides typical monitoring of CPU utilization, hard disk utilization, network input, and network output.

But what if you want to take monitoring one step further? That’s where EC2 custom metrics come in.

Why create custom metrics in CloudWatch?

CloudWatch is an effective cloud application monitoring tool, but built-in monitoring cannot do everything. For example, it does not monitor traffic on port 80 or port 443. It also does not monitor to see if your Nginx server is performing as expected.

A custom metric allows you to monitor a specific application binary or runtime. CloudWatch helps you monitor the infrastructure portion of an EC2 instance, such as CPU, hard drive, and network. However, if the application running on the instance is idle 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 review the steps to instruct CloudWatch to monitor a web server running on an EC2 Linux instance to confirm that port 443 is open for incoming HTTPS traffic. Before you begin, make sure 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, use the AWS Management Console or a script. IT teams should try to automate as much as possible in any environment, and a script is a great automation method. The automation script in this example is native to Linux and comes in Bash, but you can use any programming language for this purpose.

Create the automation script. To get started, SSH on 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 the netstat command uses to grep for port 443 to make sure it is running. Use the AWS command line interface with the cloudwatch command to create the custom metric using the put-metric-data option. The put-metric-data 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.

AWS CloudWatch Custom Metrics Automation Code

The last step is to grant the appropriate permissions for the system to run the script. Use the following chmod command:

chmod + x

Create the cron job. Once the script is saved, it’s time to run it. Make sure the script runs 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 pulls data from the EC2 instance in a reasonable manner, configure the cron job to run the script continuously.

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

Once you save the cron job, you will see a terminal output saying 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 metrics. In the CloudWatch service, click Metrics.

AWS Management Console CloudWatch Service Screens

Under All metrics, there is a new section for custom metrics.

Custom metrics

Click Custom and the new custom metric is now available.

CloudWatch custom metric available

Custom metrics with other cloud providers

There are many options to make custom metrics for those who use another cloud provider or third-party service.

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

There are also numerous monitoring tools available to Azure cloud users. Microsoft put custom metrics in preview mode for Azure Monitor. The preview version 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;
  • InfluxData Telegraf Agent; and
  • custom metrics through the Azure Monitor API.

Final remarks: How you can create EC2 custom metrics with Amazon CloudWatch

I hope you understand this article, How you can create EC2 custom metrics with Amazon CloudWatch. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.

Check Also

Download Total Commander latest version in English


Leave a Reply

Your email address will not be published. Required fields are marked *