Reporting custom metrics using the AWS CLI

We can use the AWS command line to report metrics to the CloudWatch environment. First, we need to make sure the account that we are using has permissions to post to CloudWatch. When automating, it is always preferable to attach the permissions to the EC2 instance role than to hard-code the credentials into the EC2 instance.

To report metrics, we can use the aws cloudwatch put-metric-data command within the instance that is able to read the metrics being written to CloudWatch. The required operators are as follows:

  • --metric-name: The name of the metric
  • --namespace: The namespace the metric will be recorded to
  • --value: The current value that was collected from the system
  • --timestamp: The time and date when the value was collected

Putting it all together would look something like this:

aws cloudwatch put-metric-data --metric-name WebsiteVisitors --namespace markocloud --value 37 --timestamp 2019-01-09T20:02:17.344Z

As you can see, it is fairly easy to script the delivery of custom metrics via the AWS CLI itself. In a real-world scenario, the value and timestamp would be represented as variables sourced from a certain application and the operating system itself, of course.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset