Skip to main content

Command Palette

Search for a command to run...

Aws Cloud Watch

Published
•5 min read•View as Markdown

🚀What is AWS CloudWatch?

AWS CloudWatch is a powerful monitoring and observability service provided by Amazon Web Services. It enables you to gain insights into the performance, health, and operational aspects of your AWS resources and applications. CloudWatch collects and tracks metrics, collects and monitors log files, and sets alarms to alert you on certain conditions.

📌Advantages

✅Comprehensive Monitoring: CloudWatch allows you to monitor various AWS resources such as EC2 instances, RDS databases, Lambda functions, and more. You get a unified view of your entire AWS infrastructure.

✅Real-Time Metrics: It provides real-time monitoring of metrics, allowing you to respond quickly to any issues or anomalies that might arise.

✅Automated Actions: With CloudWatch Alarms, you can set up automated actions like triggering an Auto Scaling group to scale in or out based on certain conditions.

✅Log Insights: CloudWatch Insights lets you analyze and search log data from various AWS services, making it easier to troubleshoot problems and identify trends.

✅Dashboards and Visualization: Create custom dashboards to visualize your application and infrastructure metrics in one place, making it easier to understand the overall health of your system.

🎯Problem Solving with AWS CloudWatch:

📍CloudWatch helps address several critical challenges, including:

✅Auto Scaling: CloudWatch can trigger Auto Scaling actions based on defined thresholds. For example, you can automatically scale in or out based on CPU utilization or request counts.

✅Resource Monitoring: Monitor EC2 instances, RDS databases, DynamoDB tables, and other AWS resources to gain insights into their performance and health.

✅Application Insights: Track application-specific metrics to monitor the performance of your applications and identify potential bottlenecks.

✅Log Analysis: Use CloudWatch Logs Insights to analyze log data, identify patterns, and troubleshoot issues in real-time.

✅Billing and Cost Monitoring: CloudWatch can help you monitor your AWS billing and usage patterns, enabling you to optimize costs.

🚀Practical Use Cases of AWS CloudWatch:

✅Auto Scaling: CloudWatch can trigger Auto Scaling actions based on defined thresholds. For example, you can automatically scale in or out based on CPU utilization or request counts.

✅Resource Monitoring: Monitor EC2 instances, RDS databases, DynamoDB tables, and other AWS resources to gain insights into their performance and health.

✅Application Insights: Track application-specific metrics to monitor the performance of your applications and identify potential bottlenecks.

✅Log Analysis: Use CloudWatch Logs Insights to analyze log data, identify patterns, and troubleshoot issues in real-time.

✅Billing and Cost Monitoring: CloudWatch can help you monitor your AWS billing and usage patterns, enabling you to optimize costs.

💡DEMO:

🧪step 01: Go to the AWS console search service called “Cloud watch”

🧪step 02: Create a Ec2 instance

🧪step03: Click launch instance

🧪step04: Enter name and other details

🧪step05 : Choose image , instance type

🧪step06 : Enter keypair, default security group and click launch instance

🧪step07: Instance Created🤩

🧪step08 : open terminal , connecting the local machine to remote server by using the SSH client command ,In the security groups SSH port must be enabled.

🧪step 09: Type the command “top” it shows the detailed CPU Utilization

🧪step 10: Already instance created! go to the aws cloud watch console, choose the section “ALL metrics” in below browse tab is available, choose the cpu utilization it shows, how cpu utilized , and other details.

🧪step11: Above steps or other way to see the details in the EC2 instance below tab called “monitoring” its also show the details all about CPU Utilization , memory utilization etc.,

🧪step 12:click the monitoring section-> managed detailed monitoring ->click enable->confirm

🧪step 13: create a one file in remote server called”cpu_spike.py” , using nano editor, vim editor.

🧪step 14: enter the below 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)

🧪step 15: here enter the code

🧪step 16 :Save it , prerequisites: python3 installed

🧪step 17: Run the file , using the command “python3 cpu_spike.py”

🧪step 18: It collect the metrics , it shows the graph( cpu utilization)

🧪step 19: It shows 80% completed

🧪step 20: Here 80% completed bar graph

🧪step 21: create a alarm , if 80% completed it triggers a alarm by using SNS topic

in AWS cloud watch console , in side dashboard-> Alarms ->create alarms

🧪step 22: select the metric , if my case i created the EC2 instance , i choose that instance

🧪step 23: Here choose instance cpu utilization and select metric

🧪step 24: Enter the name, metric name , statistic and period details. By demo purpose, I choose maximum and 1 minutes.

🧪step 25:And Select some conditions in my case 50% alert , so entered in 50 and click next.

🧪step 26: In notification section , In First time create a new topic , enter my email id, create topic it will generate the topic.

🧪step 27: Here created topic.

🧪step 28: Other details by default and click next

🧪step 29: Add name and description -> click next

🧪step 30 : preview and create

🧪step 31: create Alarms , collect the metrics

🧪step 32: mail triggered for subscription confirmation->click confirm subscription

🧪step 33: Once clicked confirm subscription it shows,

🧪step 34: Run the file

🧪step 35:Once reach the limit ,mail triggered -utilized for 50% percent

🧪step 36: Alarm created🤩