How to Create a CloudWatch Alarm in AWS
To create a
CloudWatch alarm, define the metric to monitor, set the threshold and evaluation period, and specify the actions to take when the alarm state changes. You can create alarms using the AWS Management Console, AWS CLI, or AWS SDKs by specifying these parameters.Syntax
The basic syntax to create a CloudWatch alarm using AWS CLI is:
aws cloudwatch put-metric-alarm: Command to create or update an alarm.--alarm-name: Unique name for the alarm.--metric-name: The metric to monitor (e.g., CPUUtilization).--namespace: The metric namespace (e.g., AWS/EC2).--statistic: Statistic type (e.g., Average, Sum).--period: Time in seconds for each data point.--threshold: Value to compare the metric against.--comparison-operator: How to compare metric and threshold (e.g., GreaterThanThreshold).--evaluation-periods: Number of periods to evaluate.--alarm-actions: Actions to take when alarm triggers (e.g., SNS topic ARN).
bash
aws cloudwatch put-metric-alarm \ --alarm-name MyAlarm \ --metric-name CPUUtilization \ --namespace AWS/EC2 \ --statistic Average \ --period 300 \ --threshold 80 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 2 \ --alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopic
Example
This example creates a CloudWatch alarm that triggers when the average CPU utilization of an EC2 instance exceeds 80% for two consecutive 5-minute periods. It sends a notification to an SNS topic.
bash
aws cloudwatch put-metric-alarm \ --alarm-name HighCPUUtilization \ --metric-name CPUUtilization \ --namespace AWS/EC2 \ --statistic Average \ --period 300 \ --threshold 80 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 2 \ --alarm-actions arn:aws:sns:us-east-1:123456789012:NotifyMe
Output
Successfully put metric alarm 'HighCPUUtilization'
Common Pitfalls
- Using incorrect metric names or namespaces causes the alarm to never trigger.
- Setting too short
periodor too fewevaluation-periodscan cause false alarms. - Not specifying
alarm-actionsmeans no notification or response happens when alarm triggers. - For SNS actions, ensure the SNS topic exists and you have permission to publish.
bash
aws cloudwatch put-metric-alarm \ --alarm-name FaultyAlarm \ --metric-name CPU_Utilization \ --namespace AWS/EC2 \ --statistic Average \ --period 60 \ --threshold 80 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 1 # Corrected version: aws cloudwatch put-metric-alarm \ --alarm-name CorrectAlarm \ --metric-name CPUUtilization \ --namespace AWS/EC2 \ --statistic Average \ --period 300 \ --threshold 80 \ --comparison-operator GreaterThanThreshold \ --evaluation-periods 2 \ --alarm-actions arn:aws:sns:us-east-1:123456789012:NotifyMe
Quick Reference
Remember these key points when creating CloudWatch alarms:
- Choose the right metric and namespace for your resource.
- Set a meaningful threshold and evaluation period to avoid noise.
- Always specify alarm actions like SNS notifications or Auto Scaling triggers.
- Test your alarm by simulating metric changes if possible.
Key Takeaways
Define the metric, threshold, and evaluation period clearly to create an effective CloudWatch alarm.
Specify alarm actions like SNS topics to get notified when the alarm triggers.
Use correct metric names and namespaces to ensure the alarm monitors the right data.
Avoid too short periods or too few evaluation periods to reduce false alarms.
Test alarms after creation to confirm they behave as expected.