Open In App

How to Create Alarm to Terminate an EC2 Instance using the Amazon CloudWatch?

Last Updated : 20 Sep, 2023
Like Article

Amazon CloudWatch Alarms are a feature of AWS(Amazon Web Services) that allows you to monitor various metrics and take automated actions based on the conditions, you define. CloudWatch Alarms help you keep track of the health and performance of your AWS resources and Applications.

Different States Of Metric Alarm

There are three possible states for the metric alarm

  • OK: The measurement or expression falls within the specified range.
  • Alarm: The measurement or expression is outside the set limit.
  • Insufficient: The metric is not accessible, the alarm has just started, or there is not enough data to allow the metric to identify the alarm state.

You can set up a CloudWatch alert to keep track of your instances’ CloudWatch metrics. If you choose a threshold, CloudWatch will notify you immediately when the measure hits that level. Using the more complex options offered by the CloudWatch console or the Amazon EC2 console, you can set a CloudWatch alarm.

Steps to Create An Alarm To Terminate An Instance Using The Amazon CloudWatch Alarm

To create an AWS free tier account refer to Amazon Web Services (AWS) – Free Tier Account Set up.

Step 1: Create an EC2 instance in AWS.

Step 2: Copy the instance ID and open the CloudWatch Console.

Cloud Watch

Step 3: choose Alarms then click Create Alarm.


Step 4: Follow the steps In the Select metric paste the instance id in the metric search box and choose pre-instance the CPUUtilization metric. Click Select metric.

>Select metric

Step 5: Follow the following steps: In the Statistic, choose Average. choose a period (5 minutes).

C) In Condition:

1. select threshold type:- Static

2. select whenever CPUUtilization is:- Greater/Equal

3. then…. give the value:-40

D) Choose next.

Conditions in cloud watch

Step 6: Follow the step to configure under the notification:-

A) for sending notifications choose an existing SNS topic or Create a New one.

B) To create an SNS topic:-

1. Choose New

2. give the topic name

3. notify the email address when the alarm changes to the alarm State.

4. Email address is sent a topic subscription confirmation email.

5. you must confirm the subscription before notification can be sent to an email address.

6. create a topic

Configure Actions

Step 7: Choose EC2 action:-

A) Whenever this alarm is, Choose State:- an alarm

B) Choose Terminate this Instance option.

C) choose next

alarm trigger

Step 8: Give alarm Name:- GFG_Alarm, choose next, and create alarm.

Create Alarm

Step 9: Alarm is created

Alarm created

Step 10: Then go to EC2 Instance console and select your EC2 Instance and connect your EC2 Instance with the OS.

Connect to EC2 instance

click the connect button

Step 11: In shell prompt, write these two commands to start CPUUtilization.

yes > /dev/null &

next enter and write this command top. And started the CPUUtilization,

Step 12: Then CPUUtilization is completed, that time alarm changes its state to in Alarm and Terminate the EC2 Instance.

CPU Utilization is completed.
Alarm changes its state :OK to In Alarm.
EC2 Instance is terminated automatically.

Benefits of the AWS CloudWatch Alarm include

  • Continuous Monitoring: You can keep an eye on AWS resources in real time thanks to CloudWatch Alarms. You can configure alarms depending on parameters like CPU utilization, memory consumption, network traffic, etc., and get prompt notifications when these metrics go beyond predetermined levels.
  • Automated Reactions: You can program automatic responses to alarms, such as sending alerts via Amazon SNS (Simple Notification Service), running AWS Lambda tasks, or even suspending or terminating instances. This makes it possible for you to react quickly and appropriately to urgent situations.
  • Elasticity and Scalability: CloudWatch Alarms assist you in controlling the elasticity and scalability of your resources. The capacity of your resources can be dynamically adjusted based on demand, for example, by setting up alerts to go off when specific metrics surpass predetermined levels.
  • Adjustable Thresholds: According to the requirements of your application, you can specify the CloudWatch Alarm thresholds. You can make sure that you’re only alerted when pertinent circumstances are met with the help of this tweak.
  • Dimension: Metrics: You can configure alarms for multi-dimensional metrics with CloudWatch Alarms. This is especially helpful in complex applications when you want to simultaneously monitor the performance of several parts of your resources.
  • Integrating Other AWS Services: Other AWS services can be linked with CloudWatch Alarms. For instance, you can use them along with AWS Auto Scaling to start scaling operations based on specific circumstances.
  • Centralized Observation: To monitor and manage alerts across diverse AWS services and resources, CloudWatch alerts offers a consolidated platform. The process of troubleshooting and monitoring is made easier by this.
  • Individual dashboards: It is simpler to gain an overview of the health and performance of your resources when using custom dashboards that you may construct in CloudWatch to show alerts and metrics in one location.
  • Cost Administration: You can monitor your expenditures and get notifications if your costs exceed predetermined budgets by configuring CloudWatch Alarms for indicators linked to costs.
  • Adaptability: Because of their adaptability, CloudWatch Alarms may be used to keep an eye on a variety of AWS resources, such as EC2 instances, RDS databases, Lambda functions, and more.
  • Statistical Analysis: To predict and identify odd behavior in your data, CloudWatch Alarms can be configured to employ Machine Learning insights, such as Amazon CloudWatch Anomaly Detection.

FAQs On CloudWatch Alarm

1. What Is Amazon CloudWatch Alarm?

AWS offers a monitoring and alerting solution called Amazon CloudWatch Alarms. You can use it to set alarms for a variety of metrics gathered from your AWS resources. An alert is set off when a metric crosses a predetermined threshold, and you may configure it to take automatic action.

2. How Can I Make A CloudWatch Alarm?

The AWS Management Console, AWS Command Line Interface (CLI), or AWS CloudFormation templates can all be used to establish a CloudWatch Alarm. A metric must be chosen, threshold values must be established, and actions must be specified for when the alert state changes.

3. What Metrics Can I Use CloudWatch To Create Alarms For?

Depending on the AWS resource you’re watching, CloudWatch supports a wide variety of metrics. Alarms can be set for things like CPU usage, network traffic, disk space, request rates, and more.

4. Is It Possible To Specify More Than One Action For A Single CloudWatch Alarm?

You can set up various actions for a single CloudWatch Alarm, yes. This enables you to execute various automated responses in reaction to the alert threshold being crossed.

5. How Do I Scale Up CloudWatch Alarm Management?

Utilizing AWS CloudFormation templates, AWS Systems Manager, and automation scripts, you can manage CloudWatch Alarms at scale. These tools make it simple for you to easily create, edit, and delete alerts across various resources.

6. What Are The Costs Associated With CloudWatch Alarms?

The amount of alerts you generate, the number of actions taken by alarms, and the frequency of API requests all affect how much a CloudWatch Alarm costs. Free tiers are offered, and prices change depending on consumption.

7. How Can I Display Metrics And Alarms From CloudWatch?

Custom representations of CloudWatch Alarms and metrics can be made using Amazon CloudWatch Dashboards. Dashboards give you a centralized view of the data you are monitoring.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads