DEV Community

piyush desai
piyush desai

Posted on

Automated Alerts for High CPU Utilization: Real-Time Email Notifications with Instance Details

Create Instance:
Create an Instance.
Step 1: Create Lambda

  • Go to the AWS Lambda console.
  • Create a new function:
  • Click Create function.
  • Choose Author from scratch.
  • Provide a name (e.g., SendAlarmNotification).
  • Select a runtime (e.g., Python 3.x).
  • Set the permissions (create a new role with basic Lambda permissions).
  • Click on Create Function.
  • Add code to send a notification: Here’s an example code snippet that you can use in your Lambda function:
import json
import boto3
import datetime

# Initialize the Boto3 clients
cloudwatch_client = boto3.client('cloudwatch', region_name='ap-south-1')
sns_client = boto3.client('sns', region_name='ap-south-1')

# Constants
INSTANCE_ID = 'i-06b205998588d9c99'  # Your EC2 instance ID
SNS_TOPIC_ARN = 'arn:aws:sns:ap-south-1:533267114782:AlarmNotificationTopic'  # Your SNS topic ARN

def lambda_handler(event, context):
    # Get CPU utilization
    cpu_utilization = get_cpu_utilization(INSTANCE_ID)

    # Construct the subject and message body
    subject = f"Your Server's CPU is {cpu_utilization}%. Please check"
    message = f"""
    Hi,

    The server CPU usage is high. Please check.

    Instance ID: {INSTANCE_ID}
    CPU Utilization: {cpu_utilization}%
    """

    # Publish the message to SNS
    response = sns_client.publish(
        TopicArn=SNS_TOPIC_ARN,
        Message=message.strip(),
        Subject=subject
    )

    return {
        'statusCode': 200,
        'body': json.dumps('CPU Utilization notification sent to SNS!')
    }

def get_cpu_utilization(instance_id):
    # Get the CPU utilization metric from CloudWatch
    response = cloudwatch_client.get_metric_statistics(
        Namespace='AWS/EC2',
        MetricName='CPUUtilization',
        Dimensions=[{
            'Name': 'InstanceId',
            'Value': instance_id
        }],
        StartTime=datetime.datetime.utcnow() - datetime.timedelta(minutes=10),
        EndTime=datetime.datetime.utcnow(),
        Period=300,
        Statistics=['Average']
    )

    # Get the average CPU utilization and convert to integer
    if len(response['Datapoints']) > 0:
        return int(round(response['Datapoints'][0]['Average']))  # Round and convert to int
    return 0  # Return 0 if no data points are available

Enter fullscreen mode Exit fullscreen mode

Step 2: Create SNS and Subscribe it

  • Go to the Amazon SNS Console:
  • Navigate to the Amazon SNS Console.
  • Create a Topic:
  • Click on Topics in the left sidebar.
  • Click on Create topic.
  • Select Standard as the topic type.
  • Enter a name for the topic (e.g., CpuUtilizationAlerts).
  • Click Create topic.
  • Note the Topic ARN for later use.
  • Subscribe Your Email to the SNS Topic
  • Open the Topic:
  • Click on the topic you just created (e.g., CpuUtilizationAlerts).
  • Create a Subscription:
  • Click on Create subscription.
  • In the Protocol dropdown, select Email.
  • In the Endpoint field, enter the email address where you want to receive notifications.
  • Click Create subscription.
  • Confirm the subscription.
  • Check your email for a subscription confirmation from Amazon SNS and confirm the subscription by clicking the link provided in the email.

Step 3: Add Cloud watch Alarm:

  • Go to the CloudWatch Console:
  • Navigate to the Amazon CloudWatch Console.
  • Create an Alarm:
  • Click on Alarms in the left sidebar.
  • Click Create Alarm.
  • Choose Select metric.
  • Navigate to EC2 > Per-Instance Metrics, and select CPUUtilization for your instance.
  • Click Select metric.
  • Set the Alarm Conditions:
  • Set the threshold (e.g., Greater than 50% for 1 consecutive period of 5 minutes).
  • Click Next.
  • Configure Actions:
  • Under Configure actions, select Lambda function.
  • Choose your Lambda function (CPUMonitorFunction) from the dropdown.
  • Review and Create the Alarm:
  • Click Next, review your settings, and click Create alarm.

Step 4: Lambda Permission Settings

  • In the AWS Lambda Console, navigate to your function’s configuration.
  • Under the Permissions section, find Resource-based policy statements.
  • Select Add permissions and choose AWS service.
  • Select Other from the dropdown list.
  • Add a unique statement ID for tracking.
  • For Principal, add lambda.alarms.cloudwatch.amazonaws.com.
  • Under Source ARN, add the ARN of your CloudWatch alarm.
  • In the Action dropdown, select lambda:InvokeFunction.
  • Click Save to apply the changes.

Step 5: Increase the CUP utilization in Instance using Python script

  • In AWS instance, Create a python file : cpu-utilization.py
  • Add the below code to cpu-utilization.py
import multiprocessing
import time

def cpu_stress():
    # This function performs an infinite loop to stress the CPU
    while True:
        pass  # Infinite loop to simulate CPU load

if __name__ == "__main__":
    # Number of CPU cores to stress
    num_cores = multiprocessing.cpu_count()

    print(f"Starting CPU stress on {num_cores} cores...")

    # Create one process per CPU core
    processes = []
    for i in range(num_cores):
        p = multiprocessing.Process(target=cpu_stress)
        processes.append(p)
        p.start()

    # Let the stress run for a certain amount of time
    time_to_run = 300  # Run for 5 minutes (300 seconds)
    time.sleep(time_to_run)

    # Stop all processes after the specified time
    for p in processes:
        p.terminate()

    print("CPU stress test completed.")

Enter fullscreen mode Exit fullscreen mode
  • Now run the File using python3 cpu-utilization.py and you will receive the notification of Ec2 cpu utilization and ram at the email.

You may further edit the subject of email using Lamda.

Copyright ~ Piyush Desai
Connect2me - https://www.linkedin.com/in/piyush-desai-/

Top comments (0)