0
0
AWScloud~5 mins

CloudWatch alarms in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
CloudWatch alarms watch your cloud resources and tell you when something needs attention. They help you catch problems early by sending alerts based on rules you set.
When you want to know if your server CPU is too high for a long time
When you need to get notified if your website is not responding
When you want to track if your database storage is almost full
When you want to automatically fix problems by triggering actions when an alarm fires
When you want to keep an eye on costs by monitoring resource usage
Config File - cloudwatch-alarm.json
cloudwatch-alarm.json
{
  "AlarmName": "HighCPUUsageAlarm",
  "AlarmDescription": "Alarm when CPU exceeds 80% for 5 minutes",
  "ActionsEnabled": true,
  "AlarmActions": ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
  "MetricName": "CPUUtilization",
  "Namespace": "AWS/EC2",
  "Statistic": "Average",
  "Dimensions": [
    {
      "Name": "InstanceId",
      "Value": "i-0abcdef1234567890"
    }
  ],
  "Period": 300,
  "EvaluationPeriods": 1,
  "Threshold": 80.0,
  "ComparisonOperator": "GreaterThanThreshold"
}

This JSON file defines a CloudWatch alarm named HighCPUUsageAlarm. It watches the average CPU usage of a specific EC2 instance over 5 minutes (300 seconds). If the CPU goes above 80%, the alarm triggers. It sends a notification to an SNS topic NotifyMe. The Dimensions specify which EC2 instance to watch by its ID.

Commands
This command creates or updates the CloudWatch alarm using the JSON file. It sets the rules and notification actions.
Terminal
aws cloudwatch put-metric-alarm --cli-input-json file://cloudwatch-alarm.json
Expected OutputExpected
No output (command runs silently)
--cli-input-json - Specifies the JSON file with alarm configuration
This command checks the details and status of the alarm named HighCPUUsageAlarm to confirm it was created.
Terminal
aws cloudwatch describe-alarms --alarm-names HighCPUUsageAlarm
Expected OutputExpected
{ "MetricAlarms": [ { "AlarmName": "HighCPUUsageAlarm", "AlarmDescription": "Alarm when CPU exceeds 80% for 5 minutes", "ActionsEnabled": true, "OKActions": [], "AlarmActions": [ "arn:aws:sns:us-east-1:123456789012:NotifyMe" ], "InsufficientDataActions": [], "StateValue": "OK", "StateReason": "Threshold Crossed: 1 datapoint [0.0 (27/04/24 10:00:00)] was not greater than the threshold (80.0).", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0abcdef1234567890" } ], "Period": 300, "EvaluationPeriods": 1, "Threshold": 80.0, "ComparisonOperator": "GreaterThanThreshold" } ] }
--alarm-names - Filters output to show only the specified alarm
This command lists all CPU usage metrics for EC2 instances, helping you find the right instance ID to monitor.
Terminal
aws cloudwatch list-metrics --namespace AWS/EC2 --metric-name CPUUtilization
Expected OutputExpected
{ "Metrics": [ { "Namespace": "AWS/EC2", "MetricName": "CPUUtilization", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0abcdef1234567890" } ] } ] }
--namespace - Filters metrics by service namespace
--metric-name - Filters metrics by metric name
Key Concept

If you remember nothing else from this pattern, remember: CloudWatch alarms watch specific metrics and alert you when those metrics cross thresholds you set.

Common Mistakes
Using the wrong instance ID in the alarm dimensions
The alarm will watch the wrong resource and never trigger for your intended server
Always verify the instance ID by listing metrics or checking your EC2 instances before creating the alarm
Not enabling actions in the alarm configuration
The alarm will detect problems but will not send notifications or trigger actions
Set ActionsEnabled to true and specify AlarmActions like SNS topics to get alerts
Setting the threshold too high or too low without testing
You may get too many false alarms or miss real problems
Start with reasonable thresholds and adjust based on observed metric behavior
Summary
Create a CloudWatch alarm by defining a JSON file with metric, threshold, and actions.
Use the AWS CLI to apply the alarm configuration with put-metric-alarm.
Check alarm status and details with describe-alarms to confirm it works.