Aws Cloud Watch Deep Dive

Aws Cloud Watch Deep Dive

Amazon CloudWatch is a monitoring and management service provided by Amazon Web Services (AWS). It allows users to collect and track metrics, collect and monitor log files, and set alarms. CloudWatch provides insights into the performance, operational health, and resource utilization of your AWS resources.

Key features of Amazon CloudWatch include:

  1. Metrics and Alarms: CloudWatch collects and stores data in the form of metrics, which are numerical data points associated with a resource over time. You can set up alarms to be notified when a particular metric breaches a specified threshold.

  2. Dashboards: Users can create custom dashboards to visualize and monitor key metrics and resources at a glance.

  3. Logs: CloudWatch Logs allow you to aggregate, monitor, and store logs from your applications and AWS resources. This helps in troubleshooting and understanding the behavior of applications.

  4. Events: CloudWatch Events enables automated responses to changes in your AWS resources. You can define rules to trigger automated actions when certain events occur.

  5. Insights: CloudWatch Insights provides the ability to interactively analyze and visualize logs for deeper insights into your system's behavior.

  6. Retrieval and Storage of Metrics Data: CloudWatch provides a robust API for retrieving metrics data, making it possible to integrate with other AWS services and third-party applications.

  7. Integration with other AWS Services: CloudWatch integrates with various AWS services, allowing you to monitor resources such as EC2 instances, S3 buckets, RDS databases, and more.

Overall, CloudWatch is a crucial component for managing and monitoring the health and performance of your AWS resources, helping to ensure the reliability and efficiency of your applications and infrastructure deployed on AWS.

DEMO - LIVE EC2 CPU ALERTING THROUGH SNS

Step 1: Create an EC2 Instance

First, you'll need an EC2 instance to monitor. Launch an EC2 instance in your AWS account.

Script for CPU Spike:

https://github.com/iam-veeramalla/aws-devops-zero-to-hero/tree/main/day-16

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)

Step 2: Access the CloudWatch Console

  1. Go to the AWS Management Console.

  2. In the "Find Services" search bar, type "CloudWatch" and select it.

Step 3: Navigate to Metrics

  1. In the CloudWatch console, click on "Metrics" in the left navigation pane.

  2. Under the "Browse" tab, select "EC2" as the namespace.

  3. Choose the specific metric you want to monitor, such as "Per-Instance Metrics" and then "CPUUtilization."

Step 4: Create an Alarm

  1. In the "CPUUtilization" metrics graph, click the "Create Alarm" button.

  2. Set the conditions for the alarm. For example, you might set a threshold of 50% CPU utilization.

  3. Configure the actions to be taken when the alarm state is triggered. You can choose to receive notifications via Amazon SNS (Simple Notification Service), for instance.

Step 5: Review and Create

  1. Review your alarm configuration.

  2. Give your alarm a name and description.

  3. Click "Create Alarm."

Subscriber of topic must confirm the subscription in order to receive the notifications.

After confirmation this msg'll appear.

status of alarm would be updated to "OK" once subscription confirmed by the subscriber of topic.

Step 6: Trigger the Alarm

To test the alarm, you can intentionally stress your EC2 instance by running resource-intensive tasks or processes.

Step 7: Monitor the Alarm

Once the CPU utilization breaches the specified threshold, the alarm will change its state. You can view the alarm state changes and any associated notifications in the CloudWatch console.

Subscriber will receive the alarm notification once the alarm trigger due to instance's CPU utilization reaches it's threshold limit.

Thank you for reading! Happy Learning!!

Santosh Chauhan

Did you find this article valuable?

Support Santosh Chauhan's blog by becoming a sponsor. Any amount is appreciated!