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
Interestingly,
id
in themetric_query
clause seems a lot less variable than implied by the documentation. Someone in this Github thread mentioned changing aroundid
is problematic, and after changing myid
values to "m1" and "e1" like the documentation example shows fixed my issue: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 :)
As you can see in this topic,
id
must start with lowercase.Try to change
IncomingRecords
toincomingRecords
andExpectedIncomingRecords
toexpectedIncomingRecords
.From docs:
Also I see you used twice
return_data = true
and you can only use it in onemetric_query
, as you can read on this topic.