Skip to main content

CloudWatch Alarm Setup

Objective#

Create a CloudWatch alarm that targets CPU utilization over 80%, sends an alert using SNS, and tests the alarm with a CPU stress test. Additionally, modify the CloudWatch alarm action to reboot the EC2 instance when CPU usage exceeds 80%.

GitHub Repo: CloudWatch Alarm Repo

Steps#

1. Create an SNS Topic#

  • Go to the AWS Management Console and navigate to the Amazon SNS service.
  • Click on "Create topic" and provide a name and display name for the topic.
  • Once the topic is created, note down its ARN (Amazon Resource Name).

2. Create a CloudWatch Alarm#

  • Go to the AWS Management Console and navigate to the Amazon CloudWatch service.
  • In the left navigation pane, click on "Alarms" and then click on "Create alarm".
  • Select the EC2 instance or Auto Scaling group for which you want to monitor CPU utilization.
  • Under "Conditions," select "Static" for the threshold type and specify the following values:
    • Threshold type: Static
    • Whenever: CPUUtilization
    • Is: Greater
    • Than: 80
    • For at least: 5 consecutive periods of 1 minute(s)
  • Under "Actions," click on "Add notification" and select the SNS topic you created earlier.
  • Click on "Create alarm" to save the settings.

3. Test the CloudWatch Alarm#

  • Connect to your EC2 instance or Auto Scaling group and install a CPU stress testing tool, such as stress (for Linux) or Prime95 (for Windows).
  • Use the stress testing tool to put a significant load on the CPU, causing its utilization to go above 80%.
  • Monitor the CloudWatch alarm and wait for it to trigger. Once triggered, you should receive an SNS notification indicating the CPU utilization is above 80%.

Modify CloudWatch Alarm Action#

To modify the CloudWatch alarm action to reboot the EC2 instance:

  • Go to the AWS Management Console and navigate to the Amazon CloudWatch service.
  • In the left navigation pane, click on "Alarms" and select the alarm you created.
  • Under the "Actions" tab, click on "Configure actions."
  • Add a new action to "Reboot the instance."
  • Save the changes.

Now, when the CPU utilization exceeds 80%, the CloudWatch alarm will trigger a reboot of the EC2 instance.