Cloudwatch Alarm 4xx Errors API Gateway Terraform

23 Views Asked by At

I am trying to set up Cloudwatch Alarms for an API Gateway. In this example I am trying to use the existing 4xx metric, but the Alarm never triggers even when the 4xx metric shows datapoints. I am struggling to find what I am doing wrong. At this point I just want to trigger the alarm as soon as any 4xx errors are raised, such as 404.

This is the terraform. I suspect the dimensions are wrong, but I am not sure. Thanks in advance!

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 = {
    ApiName = aws_apigatewayv2_api.my_gateway.name
    Stage = aws_apigatewayv2_stage.default.name
  }
}
1

There are 1 best solutions below

0
sobmortin354 On

I was not far off. The final terraform was changing dimensions from ApiName to ApiId and replacing unit with datapoints_to_alarm

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"
  datapoints_to_alarm = 1

  dimensions = {
    ApiId = aws_apigatewayv2_api.my_gateway.id
    Stage = aws_apigatewayv2_stage.default.name
  }
}