由于 heredoc,Cloudwatch 警报创建失败
Posted
技术标签:
【中文标题】由于 heredoc,Cloudwatch 警报创建失败【英文标题】:Cloudwatch alarm creation fails due to heredoc 【发布时间】:2021-08-07 05:05:33 【问题描述】:我正在尝试使用 terraform 创建复合 cloudwatch 警报。但不幸的是,我的 terraform 代码因以下错误而中断:
错误:创建 CloudWatch 复合警报时出错 (node-count-office-time-composite-alarm-DP-1474-desert): ValidationError:AlarmRule 不得包含前导或尾随 空格或为空 状态码:400,请求ID:272b14ae-e6bd-4e65-8bb8-25372d9a5f7c
以下是我的 terraform 代码:
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 版本。任何人都面临类似的问题,我该如何解决这个问题?谢谢
【问题讨论】:
【参考方案1】:截至 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+/", " "))
【讨论】:
【参考方案2】:我没有找到如何使heredoc 工作的解决方案。但是我暂时使用直接字符串表达式而不是heredoc块来修复它。以下是字符串表达式:
alarm_rule = "ALARM($aws_cloudwatch_metric_alarm.node_count.alarm_name) AND ALARM($aws_cloudwatch_metric_alarm.office_time.alarm_name)"
如果其他人遇到同样的问题,我希望它对他们有用。谢谢
【讨论】:
以上是关于由于 heredoc,Cloudwatch 警报创建失败的主要内容,如果未能解决你的问题,请参考以下文章