我正在使用 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]
}
After run terrraform apply, I can see the filter was created and the alarm was also created. But under the alarm section, there is no alarm listed. You can see in see in the picture, there are 2 filters, the filter on the left listed an alarm name and the one on the right just show none. The one on the left was manually created on the aws console and the one on the right was from terraform. And indeed, the one that was created on the console work like a charm, but the one on the right remains in the insufficient data state
我解决了这个问题。度量名称实际上是度量转换名称。并且 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]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)