Tips to create EC2 custom metrics with Amazon CloudWatch

Tips to create EC2 custom metrics with Amazon CloudWatch

Hello Geeky, so today we are focusing on How you can create EC2 custom metrics with Amazon CloudWatch. So please read this tutorial carefully so you may comprehend it in a better helpful way.

Guide: How you can create EC2 custom metrics with Amazon CloudWatch

When you want to monitor the performance and reliability of the EC2 model in AWS, Amazon CloudWatch can come to one first. The built-in AWS monitoring and monitoring function helps IT teams to track performance and retrieve data from many AWS cloud services and sources. For EC2 events, CloudWatch provides proxy monitoring of CPU usage, hard disk usage, network input and network configuration.

But what if you want to take the next step? That’s where EC2 custom metrics come into play.

Why create custom metrics in CloudWatch?

CloudWatch is an effective cloud monitoring application, but built-in monitoring does not 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 running as expected.

Custom metrics allow you to monitor binary application specific time or time period. CloudWatch helps you monitor the infrastructure part of the EC2 model, such as CPU, hard disk and network. However, if the application running on the model is down or in warning mode, the standard CloudWatch monitoring will not provide much information.

While CloudWatch does not have metrics for your specific usage case, you will want to implement a custom metric.

Let’s see what it takes to create a custom metric at CloudWatch. In the following step-by-step tutorial, We reviewed steps to build CloudWatch to monitor the web server running on the Linux EC2 model to confirm port 443 is open for incoming HTTPS traffic. Before you begin, make sure you have an EC2 model running Linux with a role for access to CloudWatch. You also need access to EC2 SSH for example.

Creating custom metrics

To create a custom metric in CloudWatch, you use either an AWS Management Console or a script. IT teams should try to practice as much as possible in any area, and scripts are a great way to practice. The automated script in this example is native to Linux and presented in Bash, but you can use any programming language for this purpose.

Create an exercise script. To start, SSH into 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 command to grep for port 443 to ensure it is running. Use the AWS Command Line interface with the cloud clock command to create custom metrics using the metric-data adding option. The put-metric-data option consists of four fields: metric name, sample ID you want to monitor, field name and value per metric, which is a grepping variable for port 443.

AWS CloudWatch custom metric code

The last step is to give proper permissions for the program to run the script. Use the following chmod command:

chmod + x

Create a cron job. Once you have saved the script, it is time to run. Make sure the script is always working so that the custom metrics are constantly updated in the CloudWatch console.

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

crontab -e

The cron function will open, and you are ready to set it up up cron. To ensure that the CloudWatch custom metric pulls data from the EC2 model in the correct format, set the cron function to run the script regularly.

* / 1 ****home/ec2-ol users/

Once you save the cron function, you will see a result from the terminal stating that the cron is running.

Cron service code metrics

Check out the custom metrics at CloudWatch. For the last step, open the AWS Management Console, and go to the CloudWatch service to check the metric. Under the CloudWatch service, click Metrics.

AWS Control Console CloudWatch service screen

Under All Metrics, there is a new section for Custom Metrics.

Custom metrics

Click Custom, and a new custom metric is now available.

CloudWatch custom metrics are available

Custom metrics with other cloud providers

There are several options to create custom metrics for those using another cloud provider or third party service.

In the Google Cloud Platform (GCP), for example, you can create custom metrics with the OpenCensus service, which is a collection of libraries for multiple programming languages ​​that allow you to download app 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 many monitoring tools available for efficient Azure cloud users. Microsoft put custom metrics into preview mode in 2021 for Azure Monitor. The preview feature lets you send metrics to Azure in several ways:

  • Azure Application Skills SDK;
  • Azure Monitor Pro on Linux or Windows VM;
  • Windows Azure Diagnostics extension;
  • Representative InfluxData Telegraf; we had
  • custom metrics via Azure Monitor API.


Guide about How you can create EC2 custom metrics with Amazon CloudWatch

In this guide, we told you about the How you can create EC2 custom metrics with Amazon CloudWatch; please read all steps above so that you understand How you can create EC2 custom metrics with Amazon CloudWatch in case if you need any assistance from us, then contact us.

How this tutorial or guide assisting you?

So in this guide, we discuss the How you can create EC2 custom metrics with Amazon CloudWatch, which undoubtedly benefits you.

Final note

I hope you like the guide How you can create EC2 custom metrics with Amazon CloudWatch. In case if you have any queries regards this article/tutorial you may ask us. Also, please share your love by sharing this article with your friends and family.

Trickbugs: Get the latest Trick and Tips
Enable registration in settings - general