0

我正在使用 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]
}

运行 terraform apply 后,我可以看到过滤器已创建,警报也已创建。但是在警报部分下,没有列出警报。您可以在图中看到,有 2 个过滤器,左侧的过滤器列出了警报名称,右侧的过滤器不显示。左边的那个是在 aws 控制台上手动创建的,右边的那个是来自 terraform 的。确实,在控制台上创建的那个就像一个魅力,但右边的那个仍然处于数据不足状态 过滤指标

4

1 回答 1

0

我解决了这个问题。指标名称实际上是指标转换名称。metric_transformation 中的命名空间必须与 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]
}
于 2021-05-26T18:13:36.823 回答