The AWS-Modified-Alarm is an AWS Lambda function designed to handle CloudWatch alarms triggered by Amazon EC2 instances. When an alarm is triggered, the Lambda function performs the following actions:
- Retrieves details from the CloudWatch alarm notification.
- Determines the instance ID and platform (Linux/Windows) associated with the alarm.
- Executes specific commands on the EC2 instance using AWS Systems Manager (SSM) based on the type of alarm (CPU, memory, or disk).
- Formats the command output.
- Sends an email notification with the alarm details and command output.
Before setting up and configuring this Lambda function, ensure the following prerequisites are met:
- AWS Systems Manager (SSM) Agent: Ensure that the AWS Systems Manager (SSM) agent is pre-installed and running on your Amazon EC2 instances. This agent is required for executing commands remotely on EC2 instances.
- Create a new Amazon SNS topic that will be used to send email notifications when lambda publishes are triggered. Make note of the Topic ARN.
- Package and deploy the Lambda function code to your AWS environment. Ensure that the necessary IAM permissions are granted to the Lambda function to access EC2 instances and SNS topics.
- In the Lambda function configuration, add the following environment variables:
SNS_Topic_Arn
: The ARN of the Amazon SNS topic you created in step 1.
- Adjust the Lambda function timeout to 15 minutes.
- Add a layer for the
tabulate
andhumanize
modules in the Lambda function.
- Set up CloudWatch alarms for your Amazon EC2 instances. While providing SNS details, use the created SNS topic for triggering Lambda.
- Trigger the CloudWatch alarms associated with your EC2 instances to test the Lambda function's functionality.
- This Lambda function uses Python and relies on the following Python packages:
boto3
: The AWS SDK for Python.humanize
: For formatting file sizes.tabulate
: For formatting tabular data.
Trigger the alarm associated with your Lambda function and check your email inbox for updated alarm details.
The CloudWatch Alarm Handler Lambda function provides the following benefits:
- Centralized Monitoring: Monitor multiple Amazon EC2 instances from a central location.
- Proactive Notifications: Receive email notifications when CloudWatch alarms are triggered, allowing prompt investigation and corrective action.
- Scalability: Scale the Lambda function to monitor a large number of Amazon EC2 instances.
The CloudWatch Alarm Handler Lambda function is a robust tool for monitoring Amazon EC2 instances, delivering timely email notifications with detailed instance information for triggered CloudWatch alarms, considering the operating system (Windows/Linux) and alarm types (CPU, Memory, Disk) This functionality proves instrumental in swiftly investigating the causes of alarms and promptly implementing corrective actions.
If you'd like to contribute to this project or have suggestions for improvements, please feel free to submit a pull request or open an issue on the GitHub repository. Your input and collaboration are highly valued.
This project is licensed under the MIT License. You are free to use, modify, and distribute the code for any purpose, including commercial use. See the LICENSE file for details.
Made with love by Hari Om ❤️.