本文介绍了指标过滤器未分配给指标报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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]
}
运行Terrform Apply后,我可以看到过滤器已创建,警报也已创建。但在警报部分下,没有列出警报。你可以在图中看到,有两个过滤器,左边的过滤器列出了报警名称,右边的过滤器只显示无。左边的是在AWS控制台上手动创建的,右边的是从Terraform中创建的。确实,在控制台上创建的那个非常有用,但右侧的那个仍然处于数据不足状态

推荐答案

我已修复该问题。指标名称实际上是指标转换名称。并且指标转换中的命名空间必须与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]
}

这篇关于指标过滤器未分配给指标报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:05