Terraform的aws_cloudwatch_metric_alarm中evaluation_periods是什么?
evaluation_periods参数作用详解
官方定义:"The number of periods over which data is compared to the specified threshold."
中文翻译:数据与指定阈值进行比较的周期数。
这个参数的核心作用是控制告警触发的敏感度,避免偶发的指标毛刺导致误告警。你可以把它理解为「告警触发需要满足阈值条件的连续统计周期数」,只有连续evaluation_periods个统计周期的指标都符合你设置的阈值规则,CloudWatch才会正式触发告警动作。
使用时它需要和period参数配合生效:
period参数定义单个统计周期的时长,单位为秒,常见取值为60(1分钟)、300(5分钟)等evaluation_periods定义需要连续多少个period的指标满足阈值才触发告警
配置示例1:EC2 CPU使用率告警
需求:避免单次CPU突发升高误告警,要求连续15分钟CPU平均使用率超过80%才触发告警,配置如下:
resource "aws_cloudwatch_metric_alarm" "ec2_high_cpu" { alarm_name = "production-ec2-high-cpu" comparison_operator = "GreaterThanThreshold" evaluation_periods = 3 metric_name = "CPUUtilization" namespace = "AWS/EC2" period = 300 # 单个统计周期为5分钟 statistic = "Average" threshold = 80 alarm_actions = [aws_sns_topic.alarm_notify.arn] # 其余实例维度等配置省略 }
这个配置的运行逻辑:
- 每5分钟统计一次EC2的平均CPU使用率
- 只有连续3次(即累计15分钟)统计结果都大于80%,才会推送告警通知
- 如果仅某一个5分钟周期CPU使用率突增到85%,后续周期回落至正常范围,不会触发告警
配置示例2:服务离线紧急告警
需求:服务健康检查异常时需要快速告警,允许少量误报,只要连续2分钟健康检查失败就触发告警:
resource "aws_cloudwatch_metric_alarm" "service_down" { alarm_name = "production-service-health-check-fail" comparison_operator = "LessThanThreshold" evaluation_periods = 2 metric_name = "HealthCheckStatus" namespace = "Custom/Service" period = 60 # 单个统计周期为1分钟 statistic = "Minimum" threshold = 1 alarm_actions = [aws_sns_topic.emergency_notify.arn] }
这个配置的运行逻辑:
- 每分钟统计一次服务健康检查的最低状态值(1为正常,0为失败)
- 只要连续2次统计结果都小于1(即连续2分钟健康检查失败),立刻触发紧急告警
内容的提问来源于stack exchange,提问作者johnsontroye
相关产品推荐
相关产品推荐

