How to Create Custom EC2 Metrics with Amazon CloudWatch 2021 Tips

This tutorial covers How to Create EC2 Custom Metrics with Amazon CloudWatch. We will do our best to make sure you understand this guide. I hope you will like this blog How to Create Custom EC2 Metrics with Amazon CloudWatch. If your answer is yes, please share after reading this.

Check out 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 probably come to your 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 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 cloud application monitoring tool, but built-in monitoring can’t 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 inactive 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 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 method of automation. The automation script in this example is native to Linux and is available in Bash, but you can use any programming language for this purpose.

Create the automation script. To begin with, SSH on the EC2 instance. Create a new file called https.sh for the automation code. Open the https.sh 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 find port 443 to make sure it is running. Use the AWS CLI 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 input variable for the port. 443.

AWS CloudWatch Custom Metrics Automation Code

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

chmod + x https.sh

Create the cron job. Once the https.sh 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/https.sh

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 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 plenty of options for creating custom metrics for those using 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 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.

Final Words: How to Create EC2 Custom Metrics with Amazon CloudWatch

hope you will understand this item How to Create Custom EC2 Metrics with Amazon CloudWatch, if your answer is no, you can request anything through the contact forum section linked to this article. And if your answer is yes, then share this article with your family and friends.

About Jon Moses

Check Also

Arduino – communication via Ethernet network

For many years, the creation of large computer networks ceased to be used only for …

Leave a Reply

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