skip to Main Content

I’m attempting to create a CloudWatch anomaly detector alarm for my Kinesis data stream using Terraform. I made the following setup based on the official Terraform xx_anomaly_detection example in their documentation:

resource "aws_cloudwatch_metric_alarm" "my_anomaly_detector" {
  alarm_name                = "LowIncomingRecordsAnomaly"
  comparison_operator       = "LessThanLowerThreshold"
  evaluation_periods        = 2
  threshold_metric_id       = "ExpectedIncomingRecords"
  insufficient_data_actions = []

  metric_query {
    id          = "ExpectedIncomingRecords"
    expression  = "ANOMALY_DETECTION_BAND(IncomingRecords, 2)"
    label       = "IncomingRecords (Expected)"
    return_data = "true"
  }

  metric_query {
    id          = "IncomingRecords"
    return_data = "true"
    metric {
      namespace   = "AWS/Kinesis"
      metric_name = "PutRecords.Records"
      stat        = "Sum"
      period      = 300
      unit        = "Count"
      dimensions  = {
        StreamName = "my_stream_name"
      }
    }
  }
}

However, when I run terraform apply, I get the following error:

Error: creating CloudWatch Metric Alarm (LowIncomingRecordsAnomaly):
ValidationError: Invalid metrics list
        status code: 400, request id: 64e37e0c-4f74-...

My code seems to be very comparable to the official doc example, so what would cause this type of error? Unfortunately "ValidationError 400" doesn’t say much about the actual issue

2

Answers


  1. Chosen as BEST ANSWER

    Interestingly, id in the metric_query clause seems a lot less variable than implied by the documentation. Someone in this Github thread mentioned changing around id is problematic, and after changing my id values to "m1" and "e1" like the documentation example shows fixed my issue:

    resource "aws_cloudwatch_metric_alarm" "my_anomaly_detector" {
      alarm_name                = "LowIncomingRecordsAnomaly"
      threshold_metric_id       = "e1"
      . . .
    
      metric_query {
        id          = "e1"
        expression  = "ANOMALY_DETECTION_BAND(m1, 2)"
         . . .
      }
    
      metric_query {
        id          = "m1"
        metric {
           . . . 
          }
        }
      }
    }
    

    Not sure why this is the case, as I believe these are supposed to be changeable fields, but hopefully this resolves anyone else's problem running into this :)


  2. As you can see in this topic, id must start with lowercase.
    Try to change IncomingRecords to incomingRecords and ExpectedIncomingRecords to expectedIncomingRecords.

    From docs:

    You can change the value of Id. It can include numbers, letters, and
    underscore, and must start with a lowercase letter.

    Also I see you used twice return_data = true and you can only use it in one metric_query, as you can read on this topic.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search