Amazon CloudWatch {Study Guide}

aws cloudwatch

On this post we will continue with the preparation for the AWS CERTIFIED SOLUTIONS ARCHITECT EXAM. Remember the exercises we are solving here are from AWS Certified Solutions Architect Official Study Guide: Associate Exam by Joe Baron and others.

This is the second post for exercises from chapter 5, Elastic Load Balancing, Amazon CloudWatch, and Auto Scaling. On this post in particular we will dive in the CloudWatch technology.

EXERCISE 5.2: Use an Amazon CloudWatch Metric

Launch an Amazon EC2 instance and use an existing Amazon CloudWatch metric to monitor a value.

For this steps we can launch an EC2 instance and monitor the metrics, but we will go beyond that and we will use the load balancer from exercise 5.1 and monitor the Request Count as shown below:

$ aws cloudwatch get-metric-statistics --namespace AWS/ApplicationELB --metric-name RequestCount --statistics Average  --period 60 --dimensions Name=LoadBalancer,Value=app/my-load-balancer/007856bc4fe4a9df Name=TargetGroup,Value=targetgroup/my-targets/104412491db8d5d8 --start-time 2019-04-03T14:20:00Z --end-time 2019-04-03T14:25:00Z
 {
     "Datapoints": [
         {
             "Timestamp": "2019-04-03T14:20:00Z", 
             "Average": 1.0, 
             "Unit": "Count"
         }, 
         {
             "Timestamp": "2019-04-03T14:21:00Z", 
             "Average": 0.9285714285714286, 
             "Unit": "Count"
         }, 
         {
             "Timestamp": "2019-04-03T14:22:00Z", 
             "Average": 1.0, 
             "Unit": "Count"
         }, 
         {
             "Timestamp": "2019-04-03T14:23:00Z", 
             "Average": 0.8666666666666667, 
             "Unit": "Count"
         }, 
         {
             "Timestamp": "2019-04-03T14:24:00Z", 
             "Average": 0.9333333333333333, 
             "Unit": "Count"
         }
     ], 
     "Label": "RequestCount"
 }

Create a Custom Amazon CloudWatch Metric

The idea of this exercise is to create a custom metric (lets say CPU utilization) and from the linux instance we want to monitor, push the data to AWS Cloudwatch.
We can then access this data from the AWS Cloudwatch API
We will follow the steps detailed in this AWS guide to accomplish this task

user@australtech.net:~$ aws cloudwatch put-metric-data --metric-name memory-usage --dimensions Instance=i-0c51f9f1213e63159  --namespace "Custom" --value 950
user@australtech.net:~$ aws cloudwatch put-metric-data --metric-name memory-usage --dimensions Instance=i-0c51f9f1213e63159  --namespace "Custom" --value 900
user@australtech.net:~$ aws cloudwatch put-metric-data --metric-name memory-usage --dimensions Instance=i-0c51f9f1213e63159  --namespace "Custom" --value 700
user@australtech.net:~$ aws cloudwatch put-metric-data --metric-name memory-usage --dimensions Instance=i-0c51f9f1213e63159  --namespace "Custom" --value 300

After we have put the data we can query the CloudWatch API to get the values with the following command:

user@australtech.net:~$ aws cloudwatch get-metric-statistics --namespace Custom --metric-name memory-usage --statistics Average  --period 60 --start-time 2019-04-03T19:20:00Z --end-time 2019-04-03T19:40:00Z