Aws Cloud Watch Deep Dive

ยท

6 min read

Aws Cloud Watch Deep Dive

Introduction

Amazon Web Services (AWS) offers a wide range of services to help you monitor and manage your cloud resources effectively. One of the key tools in this arsenal is AWS CloudWatch. In this article, we'll take a deep dive into AWS CloudWatch, exploring its features, use cases, and best practices to help you master the art of monitoring and managing your AWS resources.

What is AWS CloudWatch?

AWS CloudWatch is a monitoring and observability service provided by Amazon Web Services. It helps you collect and track metrics, collect and monitor log files, and set alarms. CloudWatch provides a unified view of your AWS resources, applications, and services, making it easy to monitor and troubleshoot issues in real-time.

Key Features of AWS CloudWatch:

  1. Metrics: CloudWatch allows you to collect and store performance data for your AWS resources, such as EC2 instances, RDS databases, and more. You can use these metrics to gain insights into resource utilization, performance trends, and system behaviour.

  2. Alarms: You can set up alarms in CloudWatch to automatically notify you when certain conditions are met. For example, you can receive an alert when CPU utilization on an EC2 instance exceeds a specific threshold.

  3. Logs: CloudWatch Logs enables you to centralize and monitor log data from your applications, making it easier to troubleshoot issues and perform root cause analysis.

  4. Dashboards: Create custom dashboards to visualize your metrics and logs, providing a consolidated view of your application's health and performance.

  5. Events: CloudWatch Events allow you to respond to changes in your AWS resources in real time. You can set up rules to trigger actions like invoking AWS Lambda functions or sending notifications via Amazon SNS.

Use Cases of AWS CloudWatch

AWS CloudWatch is a versatile service with a wide range of use cases:

  1. Infrastructure Monitoring: Monitor the health and performance of your infrastructure, including EC2 instances, EBS volumes, and RDS databases. Set up alarms to receive notifications for critical events.

  2. Application Performance Monitoring (APM): Use CloudWatch to gain insights into your application's performance. You can collect custom application metrics and create dashboards to visualize them.

  3. Log Analysis: Centralize and analyze logs from your applications and services, making it easier to troubleshoot issues and identify security threats.

  4. Cost Optimization: Monitor and analyze your AWS costs with CloudWatch. Set up billing alarms to receive alerts when your costs exceed predefined thresholds.

  5. Auto Scaling: Use CloudWatch alarms to trigger auto-scaling actions, ensuring that your application can handle varying workloads.

  6. Security and Compliance: Monitor and respond to security events by setting up CloudWatch alarms for unusual or suspicious activities.

Best Practices for AWS CloudWatch

To make the most of AWS CloudWatch, consider these best practices:

  1. Define Clear Monitoring Objectives: Understand what you need to monitor and set clear objectives for your monitoring strategy.

  2. Use Custom Metrics: Create custom CloudWatch metrics to monitor application-specific performance indicators.

  3. Alarms: Set up alarms for critical metrics to receive timely notifications when issues arise.

  4. Create Informative Dashboards: Build informative dashboards to visualize key metrics and trends for quick decision-making.

  5. Log Retention: Define a log retention strategy to balance cost and compliance requirements.

  6. Automate Responses: Use CloudWatch Events to automate responses to common events or incidents.

  7. Regularly Review and Optimize: Periodically review your CloudWatch configurations and metrics to ensure they align with your evolving requirements.

Project to analyze the CPU utilization using Aws cloud watch

To monitor CPU utilization using AWS CloudWatch and send an email notification when a particular threshold is reached, you can follow these steps:

  1. Launch an EC2 Instance: Launch an Amazon Elastic Compute Cloud (EC2) instance that you want to monitor for CPU utilization. Make sure the AWS Systems Manager Agent (SSM Agent) is installed on the EC2 instance, as it's required for sending email notifications.

  2. Create an SNS Topic: AWS Simple Notification Service (SNS) will be used to send email notifications. Create an SNS topic and subscribe your email address to it.

  3. Create a CloudWatch Alarm: Now, you'll create a CloudWatch alarm to monitor CPU utilization.

    • Log in to the AWS Management Console.

    • Go to the CloudWatch service.

    • In the left navigation pane, click on "Alarms" and then click the "Create Alarm" button.

    • Choose the "Select metrics" button under the "Create Alarm" wizard.

    • In the "Browse" section, select your EC2 instance and navigate to the "Per-Instance Metrics" section.

    • Choose "CPUUtilization" and click on the checkbox.

    • Click the "Select metric" button at the bottom.

  4. Define Alarm Conditions:

    • In the "Create Alarm" wizard, set your desired threshold for CPU utilization (e.g., > 90% for high utilization).

    • Choose the "Static" option under "Whenever CPUUtilization is..."

    • Configure the actions by clicking the "Add notification" button.

    • Select "New list" and then select "Create a new SNS topic."

    • Choose the SNS topic you created earlier for email notifications.

    • Customize the subject and message for your email notification.

  5. Set Up Actions:

    • After defining the alarm conditions and the SNS topic, click the "Create alarm" button.

    • You'll receive an email confirmation for your subscription to the SNS topic.

  6. Test the Alarm: To test the alarm and receive an email notification, you can temporarily increase the CPU utilization on your EC2 instance. This can be done by running resource-intensive tasks on the instance.

  7. Receive Email Notification: When the CPU utilization threshold is breached, CloudWatch will trigger the alarm, and an email notification will be sent to the subscribed email address.

  8. Adjust Threshold and Actions (Optional): If you want to change the threshold or add additional actions (e.g., scaling the EC2 instance using AWS Auto Scaling), you can edit the CloudWatch alarm settings accordingly.

  9. Regularly Monitor and Maintain: Continuously monitor your CloudWatch alarms and adjust them as needed to ensure effective resource management.

Here, we have the Python script which increases the CPU utilization of the EC2 instance. Run this script and see how the cloud watch monitors the activity happening in your AWS account and sends the email when the CPU limits exceed

Once everything is done, ssh to the EC2 instance and run vi cpu_utilization.py and paste this code

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)

Run this script using python3 cpu_utilization.py

Conclusion

AWS CloudWatch is a powerful monitoring and observability service that empowers AWS users to gain deep insights into their resources and applications. By mastering CloudWatch, you can ensure the reliability, performance, and security of your AWS workloads. Whether you are managing infrastructure, optimizing costs, or enhancing security, AWS CloudWatch is an essential tool in your AWS toolbox. Start exploring CloudWatch today and unlock the full potential of your AWS resources.

ย