Day 46: AWS CloudWatch

Day 46: AWS CloudWatch

#90daysofdevops

🚀Introduction

In our previous blog, we explored how to use AWS CodeDeploy for automating application deployments. Today, we will shift our focus to understanding AWS CloudWatch.

CloudWatch is a comprehensive monitoring service offered by AWS, designed to provide real-time insights into your AWS resources, applications, and services. Whether it's tracking metrics, setting alarms, or collecting logs, CloudWatch plays a vital role in ensuring your infrastructure runs smoothly. In this blog, we’ll dive into what CloudWatch is, its core features, and how you can use it to monitor and optimize your applications effectively.

In this blog we will see what is:

  • Logs

  • Metrics

  • Threshold

  • Dashboard

  • Alarm

  • How to create custom metrics


  1. First Create EC2 Instance

🔸Before moving forward, let's first understand what log groups are.

In CloudWatch Logs store historical data, meaning you can access logs generated in the past. This allows you to review and analyze events that happened earlier, such as errors, deployment details, or specific application behavior.

  1. After creating EC2 Instance successfully now go to the cloudwatch and select all metrices & search your instance ID, after that click on EC2

  1. Click on EC2> Pre-Instance Metrics

  1. Select CPUUtilization

  1. You can select here line, number, pie, etc. For better understanding we will select number

  1. The percentage we see in metrics in AWS CloudWatch is often referred to as a threshold.

  1. Create alarm & follow the picture’s given below.

  1. The threshold means the number you set. If you set it to 50 percent, it will send you an alert email. I have set the threshold to 50.

In the context of CloudWatch, a threshold is indeed the specific value (like 50 percent) that you define. When the monitored metric crosses this threshold, CloudWatch will trigger an alert, such as sending an email notification.

  1. Create new topic → Write your mailID → and click on Create Topic

  1. Check all the things and click on create alarm

  1. You get OK in state then it’s done. (now you can follow from point no. 19), if you are getting Insufficient data then follow the given pictures below (from point no. 12)

  1. Follow the given picture’s below if you are getting Insufficient data in state.

  2. Search SNS (Simple Notification Service) Create topic & after that create subscription

  1. Select Email in protocol to get link

  1. You can see Status is been in Pending confirmation to confirm go to the mail & click on confirm subscription

  1. You will get mail from AWS to confirm subscription open mail & click on confirm subscription

  1. Now you will get Confirmed in Status.

  2. After Following all this steps you will get OK in alarm state

  1. Now we will write spike file in python to get traffic

import time

def simulate_cpu_spike(duration=30, cpu_percent=80):
    print(f"Simulating CPU spike at {cpu_percent}%...")
    start_time = time.time()

    # Calculate the number of iterations needed to achieve the desired CPU utilization
    target_percent = cpu_percent / 100
    total_iterations = int(target_percent * 5_000_000)  # Adjust the number as needed

    # Perform simple arithmetic operations to spike CPU utilization
    for _ in range(total_iterations):
        result = 0
        for i in range(1, 1001):
            result += i

    # Wait for the rest of the time interval
    elapsed_time = time.time() - start_time
    remaining_time = max(0, duration - elapsed_time)
    time.sleep(remaining_time)

    print("CPU spike simulation completed.")

if __name__ == '__main__':
    # Simulate a CPU spike for 30 seconds with 80% CPU utilization
    simulate_cpu_spike(duration=30, cpu_percent=80)
  1. Now run python3 cpu_spike.py

  1. Create Dashboard. CloudWatch Dashboard allows you to monitor and visualize AWS metrics in real-time using various widgets for tracking application and infrastructure performance.

  1. Add widget. Select number to understand better.

  1. On dashboard

  1. You will receive an alarm message on your email after crossing 50.


🔸How to create custom metrics

Steps to configure CloudWatch Metrics on Linux Machine:

  1. Go to AWS Console-> Go To IAM -> Go to Role-> create Role-> Attach CloudWatchAgentServerPolicy -> click next-> Give Role Name-> click create role

  1. Attach created role to the EC2 instance on which you want to do configuration of CloudWatch Metrics.

Go to EC2-> Go to Security-> Go to Modify IAM Role-> Select the Role Name-> click on update IAM Role

  1. SSH into Your EC2 instance and apply following Commands

wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb

The command will download CloudWatch Agent on your EC2 machine.

  1. sudo dpkg -i -E ./amazon-cloudwatch-agent.deb

The Command will unzip the installed package

  1. sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

The Command start installation of CloudWatch Agent

  1. sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c [file:/opt/aws/amazon-cloudwatch-agent/bin/config.json](file:/opt/aws/amazon-cloudwatch-agent/bin/config.json) -s

The Command will run the AWS CloudWatch Agent

  1. sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a status

The Command Will start the AWS CloudWatch Configuration as per selected Settings.

  1. sudo systemctl status amazon-cloudwatch-agent

The Command will show the status of cloudwatch-agent is it running or not.

  1. Now monitor the instance from CloudWatch Console.

  2. Go to the cloudwatch dashboard->click on all Metrics-> click on cwagent-> click on InstanceId

  3. Select Metrics which utilization you want to check

  1. This is how you can check the memory, disk and CPU utilization of your EC2 instance.

I hope this will help you in setup of CloudWatch disk and memory utilization metrics on your Linux EC2 instance.


🔸Here are the top CloudWatch metrics and their uses:

  1. CPUUtilization: Tracks CPU usage. Helps identify under/over-utilization of EC2 instances.

  2. DiskReadOps / DiskWriteOps: Measures disk read/write operations. Useful for monitoring disk I/O performance.

  3. DiskReadBytes / DiskWriteBytes: Tracks data volume read/written to disk. Helps monitor data-intensive workloads.

  4. NetworkIn / NetworkOut: Measures incoming/outgoing network traffic. Monitors network usage.

  5. StatusCheckFailed: Monitors EC2 instance health (system and instance-level issues).

  6. MemoryUtilization: Custom metric for tracking memory usage. Detects memory pressure.

  7. Latency (ELB): Monitors load balancer latency. Helps assess response times.

  8. RequestCount (ELB): Tracks the number of requests received by an ELB. Monitors traffic levels.

These metrics help in optimizing resource usage, detecting bottlenecks, and ensuring system health


🚀Conclusion

In this blog, we covered the basics of CloudWatch, including Logs, Metrics, Thresholds, Dashboards, Alarms, and how to create custom metrics. In the next blog, we will dive into AWS Lambda.


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

Linkedln

Twitter

Github