Create a CPU usage alarm
You can create an CloudWatch alarm that sends a notification using Amazon SNS when the alarm
changes state from OK
to ALARM
.
The alarm changes to the ALARM
state when the average CPU use of an EC2 instance exceeds
a specified threshold for consecutive specified periods.
Setting up a CPU usage alarm using the AWS Management Console
Use these steps to use the AWS Management Console to create a CPU usage alarm.
To create an alarm based on CPU usage
Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
. -
In the navigation pane, choose Alarms, All Alarms.
-
Choose Create alarm.
Choose Select metric.
-
In the All metrics tab, choose EC2 metrics.
Choose a metric category (for example, Per-Instance Metrics).
-
Find the row with the instance that you want listed in the InstanceId column and CPUUtilization in the Metric Name column. Select the check box next to this row, and choose Select metric.
Under Specify metric and conditions, for Statistic choose Average, choose one of the predefined percentiles, or specify a custom percentile (for example,
p95.45
).-
Choose a period (for example,
5 minutes
). Under Conditions, specify the following:
For Threshold type, choose Static.
-
For Whenever CPUUtilization is, specify Greater. Under than..., specify the threshold that is to trigger the alarm to go to ALARM state if the CPU utilization exceeds this percentage. For example, 70.
-
Choose Additional configuration. For Datapoints to alarm, specify how many evaluation periods (data points) must be in the
ALARM
state to trigger the alarm. If the two values here match, you create an alarm that goes toALARM
state if that many consecutive periods are breaching.To create an M out of N alarm, specify a lower number for the first value than you specify for the second value. For more information, see Evaluating an alarm.
For Missing data treatment, choose how to have the alarm behave when some data points are missing. For more information, see Configuring how CloudWatch alarms treat missing data.
-
If the alarm uses a percentile as the monitored statistic, a Percentiles with low samples box appears. Use it to choose whether to evaluate or ignore cases with low sample rates. If you choose ignore (maintain alarm state), the current alarm state is always maintained when the sample size is too low. For more information, see Percentile-based CloudWatch alarms and low data samples.
Choose Next.
-
Under Notification, choose In alarm and select an SNS topic to notify when the alarm is in
ALARM
stateTo have the alarm send multiple notifications for the same alarm state or for different alarm states, choose Add notification.
To have the alarm not send notifications, choose Remove.
-
When finished, choose Next.
-
Enter a name and description for the alarm. Then choose Next.
The name must contain only UTF-8 characters, and can't contain ASCII control characters. The description can include markdown formatting, which is displayed only in the alarm Details tab in the CloudWatch console. The markdown can be useful to add links to runbooks or other internal resources.
Under Preview and create, confirm that the information and conditions are what you want, then choose Create alarm.
Setting up a CPU usage alarm using the AWS CLI
Use these steps to use the AWS CLI to create a CPU usage alarm.
To create an alarm based on CPU usage
-
Set up an SNS topic. For more information, see Setting up Amazon SNS notifications.
-
Create an alarm using the put-metric-alarm command as follows.
aws cloudwatch put-metric-alarm --alarm-name
cpu-mon
--alarm-description "Alarm when CPU exceeds 70%" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period300
--threshold70
--comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=i-12345678
--evaluation-periods2
--alarm-actions arn:aws:sns:us-east-1
:111122223333
:my-topic
--unit Percent -
Test the alarm by forcing an alarm state change using the set-alarm-state command.
-
Change the alarm state from
INSUFFICIENT_DATA
toOK
.aws cloudwatch set-alarm-stateĀ --alarm-name
cpu-mon
--state-reason "initializing" --state-value OK -
Change the alarm state from
OK
toALARM
.aws cloudwatch set-alarm-state --alarm-name
cpu-mon
--state-reason "initializing" --state-value ALARM -
Check that you have received a notification about the alarm.
-