If you want to monitor the performance and reliability of an EC2 instance on AWS, the first thing that probably comes to mind is Amazon CloudWatch. This integrated AWS monitoring and observability service helps IT teams track performance and collect data from many AWS cloud services and resources. For EC2 instances, CloudWatch provides typical monitoring of CPU usage, disk usage, network input, and network output.
But what if you want to go one step further in monitoring? This is where custom EC2 metrics come in.
Why create custom metrics in CloudWatch?
CloudWatch is an effective cloud application monitoring tool, but built-in monitoring can’t do everything. For example, it doesn’t monitor traffic on port 80 or port 443. It also doesn’t monitor whether your Nginx server is running as expected.
You can use a custom metric to monitor a specific application binary or runtime environment. CloudWatch helps you monitor the infrastructure portion of an EC2 instance, such as the CPU, disk, and network. However, if the application running on the instance is idle or in an alert state, standard CloudWatch monitoring does not provide much information.
If CloudWatch doesn’t have a metric for your specific use case, consider implementing a custom metric.
Let’s see what it takes to create a custom metric in CloudWatch. In the step-by-step guide below, we’ll walk through the steps to instruct CloudWatch to 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 map to access CloudWatch. You also need EC2 SSH access to the instance.
Create a custom metric
To create a custom metric in CloudWatch, use either AWS management console or a script. IT teams should try to automate as much as possible in any environment, and scripting is a great way to automate. The automation script in this example is native to Linux and presented in Bash, but you can use any programming language for this.
Create the automation script. First, establish an SSH connection to 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 and 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 holds the netstat Command to grep for port 443 to make sure it is running. Use AWS command line interface with the Cloudwatch Command to create the custom metric with the put-metric-data Opportunity. the put-metric-data Option includes four parameters: the metric name, the instance ID you want to monitor, the namespace, and the value for the metric which is the grepping variable for port 443.
The final step is to give the system the appropriate permissions 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 that the script runs continuously so that the custom metrics are always updated in the CloudWatch console.
Run the following command on the EC2 instance to create a cron job:
The cron job will open and you can now set up the cron. To ensure that the CloudWatch custom metric is properly pulling the data from the EC2 instance, set the cron job to run the script continuously.
*/1 * * * * /home/ec2-user/https.sh
As soon as you save the cron job, you will see output from the terminal stating that the cron is running.
Check the custom metric in CloudWatch. In the final step, open the AWS Management Console and go to the CloudWatch service to check the metric. Under the CloudWatch service, click Metrics.
Under All measured values, there is a new section for Custom Metrics.
Click on Custom, and the new custom metric is now available.
Custom metrics with other cloud providers
There are many ways to create custom metrics for those using a different cloud provider or third-party service.
For example, you can create custom metrics in Google Cloud Platform (GCP) with the OpenCensus service, a collection of multi-language libraries that you can use to collect app metrics. You can also use the GCP Cloud Monitoring API to create custom metrics that work with C #, Go, Java, Node.js, PHP, Python, and Ruby.
Numerous monitoring tools are also available for Azure cloud users. Microsoft put custom metrics into preview mode for Azure Monitor in 2021. With the preview release, you can send metrics to Azure in a number of ways:
- Azure Application Insights SDK;
- Azure Monitor Agent on Linux or Windows VMs;
- Extension for Windows Azure Diagnostics;
- InfluxData Telegraph Agent; and
- custom metrics through the Azure Monitor API.