指标过滤器未分配给指标警报

metric filter is not assigned to metric alarm

我正在使用 terraform 设置 aws cloudwatch 日志,以便在日志中出现错误时发送电子邮件。

terraform 文件如下所示:

resource "aws_cloudwatch_log_metric_filter" "error-importing-rfid-metric-filter" {
  name           = "rfid-data-import-metric-filter"
  log_group_name = var.SERVICE_NAME
  pattern        = "ERROR"
  metric_transformation {
    name      = "error-rfid-data-import"
    namespace = "DataImportErrorMetrics"
    value     = "1"
  }
}

resource "aws_cloudwatch_metric_alarm" "error-importing-rfid" {
  alarm_name          = "error-rfid-data-import"
  metric_name         = "DataImportErrorMetrics"
  threshold           = "0"
  statistic           = "Sum"
  comparison_operator = "GreaterThanThreshold"
  datapoints_to_alarm = "1"
  evaluation_periods  = "1"
  period              = "60"
  namespace           = "LogMetrics"

  alarm_actions = [aws_sns_topic.alarm.arn]
}

应用运行 terrraform 后,我可以看到过滤器已创建,警报也已创建。但是在报警部分下,并没有列出报警。你可以在图中看到,有2个过滤器,左边的过滤器列出了一个警报名称,右边的​​过滤器只显示none。左边的是在 aws 控制台上手动创建的,右边的是来自 terraform 的。的确,在控制台上创建的那个很好用,但是右边那个仍然处于数据不足状态

我解决了这个问题。度量名称实际上是度量转换名称。 metric_tranformation 中的命名空间必须与 aws_cloudwatch_metric_alarm 中的命名空间相同。正确的片段应该是

resource "aws_cloudwatch_log_metric_filter" "error-importing-rfid-metric-filter" {
  name           = "rfid-data-import-metric-filter"
  log_group_name = var.SERVICE_NAME
  pattern        = "ERROR"
  metric_transformation {
    name      = "error-rfid-data-import-metric"
    namespace = "LogMetrics"
    value     = "1"
  }
}

//set up the alarm
resource "aws_cloudwatch_metric_alarm" "error-importing-rfid" {
  alarm_name          = "error-rfid-data-import-alarm"
  metric_name         = "error-rfid-data-import-metric"
  threshold           = "0"
  statistic           = "Sum"
  comparison_operator = "GreaterThanThreshold"
  datapoints_to_alarm = "1"
  evaluation_periods  = "1"
  period              = "60"
  namespace           = "LogMetrics"

  alarm_actions = [aws_sns_topic.alarm.arn]
}