You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

API Gateway CloudWatch 4xx错误告警不触发问题排查求助

排查API Gateway CloudWatch 4xx告警未触发的问题

核心问题:维度配置不匹配

你的Terraform代码中使用了ApiName作为维度,但API Gateway v2的CloudWatch指标维度要求使用ApiId而非ApiName。AWS/ApiGateway命名空间下,v2 API的指标维度是ApiId和Stage,而非名称,这会导致告警无法匹配到正确的指标数据点。

其他排查要点

  • 确认阶段已开启指标收集:检查你的aws_apigatewayv2_stage资源是否开启了CloudWatch指标上报,需要添加metrics_enabled = true配置:
    resource "aws_apigatewayv2_stage" "default" {
      # 其他配置...
      metrics_enabled = true
    }
    
  • 验证指标维度匹配:登录CloudWatch控制台,进入AWS/ApiGateway命名空间,选择对应ApiId和Stage维度,查看4xx指标是否有数据点,确保和告警配置的维度完全一致。
  • 检查treat_missing_data设置:当前配置为"missing",会将缺失数据视为正常状态。如果你的指标数据存在间歇性缺失,可根据需求调整为"breaching"(缺失时触发告警)或"notBreaching"(缺失时视为正常),但这不是当前告警未触发的核心原因。

修正后的Terraform代码

resource "aws_cloudwatch_metric_alarm" "gateway_error_rate" {
  alarm_name          = "${local.workspace_name}-gateway-errors"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  alarm_description   = "Gateway error rate has exceeded threshold"
  treat_missing_data  = "missing"
  metric_name         = "4xx"
  namespace           = "AWS/ApiGateway"
  period              = 60
  evaluation_periods  = 1
  threshold           = 1
  statistic           = "Sum"
  unit                = "Count"

  dimensions = {
    ApiId  = aws_apigatewayv2_api.my_gateway.id  # 替换为ApiId
    Stage  = aws_apigatewayv2_stage.default.name
  }
}

内容的提问来源于stack exchange,提问作者sobmortin354

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 07:50:03