6

我正在尝试使用 terraform 在 Cloudwatch 中设置和警报。我的告警基本上需要检查网关在1分钟的2个时段内是否有超过5%的5xx错误。

我已经尝试了以下代码,但它不起作用:

resource "aws_cloudwatch_metric_alarm" "gateway_error_rate" {
  alarm_name          = "gateway-errors"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  alarm_description   = "Gateway error rate has exceeded 5%"
  treat_missing_data  = "notBreaching"
  metric_name         = "5XXError"
  namespace           = "AWS/ApiGateway"
  period              = 60
  evaluation_periods  = 2
  threshold           = 5
  statistic           = "Average"
  unit                = "Percent"

  dimensions = {
    ApiName = "my-api"
    Stage = "dev"
  }
}

即使部署了警报,也不会显示数据。做一些测试,我注意到这个警报显然不接受单位“百分比”。

有没有人有关于如何配置此类警报的示例terraformcloudformation

4

2 回答 2

5

根据Marcin评论中提供的信息,我在 aws 文档中找到了此信息:

Average 统计量表示 5XXError 错误率,即 5XXError 错误的总数除以该期间的请求总数。分母对应于 Count 指标(如下)。

我在 terraform 中配置的警报如下所示:

resource "aws_cloudwatch_metric_alarm" "gateway_error_rate" {
  alarm_name          = "gateway-errors"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  alarm_description   = "Gateway error rate has exceeded 5%"
  treat_missing_data  = "notBreaching"
  metric_name         = "5XXError"
  namespace           = "AWS/ApiGateway"
  period              = 60
  evaluation_periods  = 2
  threshold           = 0.05
  statistic           = "Average"
  unit                = "Count"

  dimensions = {
    ApiName = "my-api"
    Stage = "dev"
  }
}
于 2020-07-03T09:34:41.463 回答
0

我在 CloudFormation 上使用它,它工作正常,我使用 SUM 而不是“百分比”

  ApiGateway5XXErrorAlarm:
    Type: 'AWS::CloudWatch::Alarm'
    Properties:
      AlarmDescription: 'Api Gateway server-side errors captured'
      Namespace: 'AWS/ApiGateway'
      MetricName: 5XXError
      Dimensions:
      - Name: ApiName
        Value: !Ref ApiGateway
      - Name: Stage
        Value: dev
      Statistic: Sum
      Period: 60
      EvaluationPeriods: 1
      Threshold: 1
      ComparisonOperator: GreaterThanOrEqualToThreshold
      AlarmActions:
      - !Ref Alerts
      TreatMissingData: notBreaching
于 2020-07-03T05:53:00.553 回答