2

我正在尝试使用 terraform 创建复合 cloudwatch 警报。但不幸的是,我的 terraform 代码因以下错误而中断:

错误:创建 CloudWatch 复合警报时出错 (node-count-office-time-composite-alarm-DP-1474-desert):ValidationError:AlarmRule 不得包含前导或尾随空格或为空状态代码:400,请求 ID:272b14ae- e6bd-4e65-8bb8-25372d9a5f7c

以下是我的地形代码:

resource "aws_cloudwatch_composite_alarm" "node_count_office_time_alarm" {
  depends_on = [aws_cloudwatch_metric_alarm.node_count, aws_cloudwatch_metric_alarm.office_time]
  alarm_description = "Composite alarm for node count & office time"
  alarm_name        = "node-count-office-time-composite-alarm-${local.postfix}"
  alarm_actions = [var.sns_topic_arn]
  ok_actions    = [var.sns_topic_arn]
alarm_rule =<<-EOF
ALARM(${aws_cloudwatch_metric_alarm.node_count.alarm_name}) AND
ALARM(${aws_cloudwatch_metric_alarm.office_time.alarm_name})
EOF
}

我检查了很多次,我的 alarm_rule 中没有前导或尾随空格。AND 运算符后只有新行。我正在使用 terraform 0.15.3 版本。任何人都面临类似的问题,我该如何解决这个问题?谢谢

4

2 回答 2

2

我没有找到如何使heredoc工作的解决方案。但是我暂时使用直接字符串表达式而不是heredoc块来修复它。以下是字符串表达式:

alarm_rule        = "ALARM(${aws_cloudwatch_metric_alarm.node_count.alarm_name}) AND ALARM(${aws_cloudwatch_metric_alarm.office_time.alarm_name})"

如果他们面临同样的问题,我希望它对其他人有用。谢谢

于 2021-05-18T07:52:14.903 回答
2

截至 2021 年撰写本文时, Terraform 说明https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_composite_alarm并不准确。

alarm_rule接受单个字符串参数,必须处理 EOF/heredoc 以创建文字字符串:

locals {
  alarm_rule_with_newlines = <<-EOF
    ALARM(${aws_cloudwatch_metric_alarm.alpha.alarm_name}) OR
    ALARM(${aws_cloudwatch_metric_alarm.bravo.alarm_name})
  EOF
}

[...]

alarm_rule = trimspace(replace(local.alarm_rule_with_newlines, "/\n+/", " "))
于 2021-05-28T01:16:19.170 回答