由于heredoc,Cloudwatch警报创建失败



我正在尝试使用terraform创建一个复合cloudwatch警报。但不幸的是,我的地形代码出现以下错误:

错误:创建CloudWatch复合警报时出错(节点-办公室-时间-组件-警报-DP-1474-插入(:ValidationError:AlarmRule不能包含前导或尾随空白或为null状态代码: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运算符后的新行。我使用的是地形0.15.3版本。任何人都面临类似的问题,我该如何解决这个问题?感谢

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

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

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

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

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+/", " "))

我对提出的两个答案都不满意,所以我有另一个解决方案。将您的复合警报规则移动到单独的文件中,只需阅读即可:

alarm_rule = file("./composite-alert-rule")

alarm_rule = templatefile("./composite-alert-rule", { arg = ... })

如果您需要传递一些动态参数。查看地形图文档以供参考:https://www.terraform.io/language/functions/templatefile
https://www.terraform.io/language/functions/file

相关内容

  • 没有找到相关文章

最新更新