Skip to main content

AWS Cloud Watch

Updated 4 min readMarkdown
Part of seriesaws-cloud
AWS Cloud Watch

AWS CLoud watch is an AWs service which acutally tracks all the activities and tell us what happened when we were not around.

So basically it help is in Monitoring, Alerting, Reporting and Logging. And these are the ways which helps us to Monitor our AWS Infra.

AWS Cloud watch is capable of Both Infrastructure and Application Monitoring

AWS Cloud watch also allows to get real life metrics, for example: When you are using EC2 instance, how many API request did my application receveid. Or During last 30 minutes what was the memory consmption of CPU in my EC2 instace.

Metrices, give details Info about your Infra’s . So Cloud watch does few Importance thing

  1. Monitoring

  2. Real-life Matcies

  3. Alarms

  4. Log Insights : Logging is bacially the capabitliy to check which service is calling or using which service

  5. Custom Metrics: Ex: Cloud Watch by default track CPU utilization , but dose not tracks the Memory utilizationm this need to be manully send to cloud watch

  6. Cost optimization : Cloud Watch plays critical role for cost optimization

  7. Scaling: As CLoud watch is Gate Keeper for your infa, so basically it can be integrated with Auto Scalling groups and in case the CPU increases , then it trigger Autoscalling group , to fire another instance.

Usecase: Below is small Demo how Cloudwatch can be used.

✅ Step 1 : Create a EC2 instance t2.micro can be used.

✅ Step 2 : Go to Cloud Watch Dahsboard Under Alarm - > Create Alalrm

Then Select Metric , EC2 → Pre Instance Matrices, click on Check box for the instance you have created and Sleelct Matrices

👉 Now in Step 1 of CloudWatch Dasbboard we need to specifiy metics and condition

👉 Step 2 in cloud watch Dashboard we need to configure the action , like create a new sns topic or if there is alreday available we can use that as well, here we have created a New SNS topic

👉 step 3 in cloud watch dashboard we just Add some name and Description , of how our message should look

👉 Step 4 in Cloud Watch Dashboard is just Review and Create alarm, just press the create alarm Button at the end and the Alarm will be creted

👉 Step 5 : Now as you have created the above Coud Watch Alarm , most important part is , you need to check if the alarm is there in Cloudwatch Dashboard and it should be having a Warning Symbol next to it

This means we need to go to email ID , where the alert will be delivered and need to confirm from there

Once you confirm the subscription from the above link send on email you will see the warning symbol is removed

✅ Step 4 : Go to your EC2 Instance and run below python script, to artifically simulate the CPU spike

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)

And let it run

Once you see this is run, we can see the Email is coming up once CPU is spiked to 30%

Thanks for Reading it till here, if understood please implement the same

No comments yet. Be the first to comment.

aws-cloud

Part 18 of 24

Welcome to my dedicated AWS writing domain. Here, I break down cloud architecture, serverless systems, DevOps best practices, and practical AWS tutorials. This series centralizes all my cloud infrastructure deep dives into one unified space to help you build scalable and secure cloud applications.

Up nextAWS Custome CloudWatch & SSM (Systems Manager)AWS SSM stands for Amazon Web Services Systems Manager. It's a management service that helps you view operational data, automate tasks, and manage configurations across your AWS resources and hybrid e