AWS, Tutorials

5 Mins Read

Implementation of Custom Cloudwatch Metric in Amazon Web Services

What are metrics?

Metrics are used to monitor various resources in Amazon Web Services like EBS volumes, EC2 instances and RDS instances. Apart from the pre-defined metrics in AWS, sometimes monitoring is required for additional service parameters. For creating a user defined metric, AWS has introduced custom metrics in CloudWatch.

This feature can be used to store business and application metrics in Amazon CloudWatch.  Also, a graph plot is generated to give a visual interpretation of the metric. Custom metrics can be setup on almost all services such as Amazon EC2 instances, AWS Billing, Autoscaling, EBS volumes and Relational Database Services. In addition, CloudWatch alarms can be set and automated actions can be configured on the basis of these metrics.

 

Why use Custom metrics?

Amazon Web Services provides a wide range of metrics for EBS, EC2 and RDS services. Alarms can be configured on the basis of these metrics and actions like terminating EC2 instances, restarting them or sending messages to SQS can then be taken.

Each of these services have different metrics associated with them and Cloudwatch can measure metrics provided by the hypervisor like disk-read, disk-write and disk usage for EBS and CPU Utilization, CPU credit Usage.

For instance, a user might have to take automated actions based on a particular parameter. This parameter can be the number of active users in a system which can be stored in an RDS instance. A script can be used to measure this value and if the number of users is zero for a particular time period, an alarm needs to be triggered and the EC2 instance needs to be terminated.

 

Use Case:

For instance, a user might have to take automated actions based on a particular parameter. This parameter can be the number of active users in a system which can be stored in an RDS instance. A script can be used to measure this value and if the number of users is zero for a particular time period, an alarm needs to be triggered and the EC2 instance needs to be terminated.

Now let us see how we can configure a custom metric based on the above use case:

 

Prerequisites:

In the following steps we will configure a custom metric to monitor the active sessions of a particular user using an RDS instance and create an alarm for this custom metric to terminate EC2 instance when it reaches the threshold of zero.

Creating a custom metric

The following code snippet can either be run on python interactive console or the whole code can be copied and run using it as a python script.

 

The following code gets the active number of users in RDS running MySQL and updates the custom metric based on that value. This script can be run as a cron job to get dynamic values where for every one minute the value of the custom metric is updated.

Parameter Input parameter type Description
namespace string The namespace of the alarm to tag.
name list or string The list of names to add the alarms or a string name to add a single alarm name.
value int, float or list The list or a single int value to add to the alarm.
dimensions dictionary The name-value pairs to associate with the metric.The dimensions can either be InstanceId key-value pairs or InstanceName key-value-pair.

 

Note: For values like  CPUUtilization, AWS/EC2 cannot be used in the namespace and name field because they are pre-defined values and cannot be overridden. These values can be accessed by using the function:

The above code prints a list of metric name and metric namespace values which cannot be used in the custom metric function.

How to check whether the custom metric was created:
Step 1. Navigate to CloudWatch in the  AWS Management Console. A new dropdown menu called ‘custom metrics’ appears. Click on it.

custom_metrics_1

Step 2. Click on the drop down menu, the newly created metric’s namespace ‘my_namespace’ is displayed, click on it.

custom_metrics_3

 

Step 3. The newly created metric ‘my_metric’ for the instance ‘i-a1b2c3d4’ can be viewed.

custom_metrics_3

Creating a CloudWatch alarm for the custom metric

Now that the custom metric is created, we need to create an alarm for it. This alarm can be created  using the AWS Management console or can be done using the following script:

The following code snippet creates an alarm called ‘my_alarm’ for the custom metric. Whenever the value of the custom metric is zero for 5 minutes time period the alarm terminates the EC2 instance.

 

Parameter Input parameter type Description
name list or string The list of names to add the alarms or a string name to add a single alarm name.
comparison operators The operator to check the custom metric values
threshold int or float The value for which the alarm should take action
period int The time period for which the alarm should take action
evaluation_period int The number of times the value has passed the threshold in order for the alarm to activate
statistic string The statistical value for the metric
alarm_actions string The arn of the action to be taken once the alarm is active.

Note: The put metric data must actively supply the metric value in order for the alarm to be in ok state or else it will be in insufficient state.

 

Perform the following steps to view the alarm using the AWS Management console .

Go to AWS console and open CloudWatch and select ‘alarms’.

alarm

Using the above steps, we have created a custom metric called ‘my_metric’, gave it a static value of 10 and associated it with an EC2 instance. We also created a CloudWatch alarm called ‘my_alarm’  configured the alarm  to terminate the instance if the value of metric is zero for 5 minutes. The alarm will go in insufficient state if the custom metric has a static value. The metric created should have dynamic input at a fixed time intervals so that the alarm is in ‘ok’ state. This can be achieved by using a cron job to supply dynamic values to CloudWatch.

Pricing

Limitations:

For further queries regarding creation of custom metrics, please feel free to comment below and I will be happy to answer.

Voiced by Amazon Polly

WRITTEN BY CloudThat

CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.

SHARE

Comments

  1. Arun

    Dec 31, 2016

    Reply

    Hello,
    Is it possible to monitor a URL with the custom cloud metrics?

    • Ravi Theja

      Feb 16, 2017

      Reply

      Hi Arun,

      First of all a huge thank you for reading my blog and apologies for the delayed response. the answer to your question is no we cannot monitor a URL using custom metrics, since AWS supports only metrics with numerical values.

      But a workaround I can suggest is the HTTP response codes. You can create different namespaces in Metrics or different metrics such as 4xx code or 5xx codes for monitoring 404 or 500 errors and you can setup alarms based on the values you get frequently. Please let me know if you have any further queries I will be happy to answer them.

  2. abderrahman

    Jun 7, 2016

    Reply

    can you tell me how to cutomize metric that gather information about any running application in amazon instance using python

    • Ravi Theja

      Jun 13, 2016

      Reply

      @abderrahman Yes you can.

      Just get that value from the application and store it into a file or from python subprocess module.

      import subprocess
      application_value=subprocess.Popen(‘ls’, stdout=subprocess.PIPE)

      In order to push the data to cloudwatch we can use the put metric data as described above in my blog.

      from boto.ec2.cloudwatch import connect_to_region
      conn_cw=boto.ec2.cloudwatch.connect_to_region(reg,aws_access_key_id=’your_access_key’,aws_secret_access_key=’your_secret_key’)
      conn_cw.put_metric_data(namespace=’my_namespace’,name=’my_metric’,value=application_value,dimensions={‘InstanceId’:’i-a1b2c3d4′})

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!