Sub topic 2
Creating Metric Alarms in Amazon CloudWatch
Assume you know AWS regions, IAM permissions, EC2, the AWS CLI, and the difference between logs and metrics. This chapter shows how to alert on resource health and collect supporting data.
Creating metric alarms
A metric alarm evaluates a metric against a threshold over one or more periods. For example, this alarm enters ALARM when average CPU is at least 80 percent in two of the last three five-minute periods:
aws cloudwatch put-metric-alarm \
--alarm-name high-cpu-web \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--statistic Average \
--period 300 \
--evaluation-periods 3 \
--datapoints-to-alarm 2 \
--threshold 80 \
--comparison-operator GreaterThanOrEqualToThreshold \
--treat-missing-data notBreaching \
--alarm-actions arn:aws:sns:us-east-1:111122223333:ops-alertsevaluation-periods defines the evaluation window. datapoints-to-alarm prevents one brief spike from triggering the alarm. Choose --treat-missing-data deliberately. notBreaching avoids alerts when telemetry temporarily disappears, while breaching is safer when missing data indicates failure.
Alarm actions and SNS notifications
Amazon Simple Notification Service (SNS) distributes alarm notifications to subscribers such as email addresses, HTTP endpoints, or Lambda functions. The SNS topic must exist, and its subscribers must be confirmed. The alarm's IAM context must also be allowed to publish to the topic.
An alarm action runs when the alarm changes state. Use separate actions for ALARM, OK, and INSUFFICIENT_DATA when your response process needs to distinguish failure from missing telemetry.
Log groups, streams, and retention
A log group contains related logs, such as /app/web. A log stream is one source within that group, often one EC2 instance or container. Set retention so old events are deleted automatically instead of storing them indefinitely.
aws logs put-retention-policy \
--log-group-name /app/web \
--retention-in-days 30Collecting data with the CloudWatch agent
The CloudWatch agent collects data that EC2 does not publish by default, including memory, disk usage, and application files:
{
"agent": { "metrics_collection_interval": 60 },
"metrics": {
"append_dimensions": { "InstanceId": "${aws:InstanceId}" },
"metrics_collected": {
"mem": { "measurement": ["mem_used_percent"] },
"disk": { "measurement": ["used_percent"], "resources": ["*"] }
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/app/app.log",
"log_group_name": "/app/web",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}Install the agent, attach an instance role with CloudWatch permissions, save this configuration, and start the agent. Confirm that the expected metric dimensions and log streams appear.
Exercise
Why might an alarm with missing data treated as notBreaching fail to alert during an agent outage?
Show answer
The alarm treats absent datapoints as healthy, so it can remain OK even when the agent stops reporting.