由于 heredoc,Cloudwatch 警报创建失败
Cloudwatch alarm creation fails due to heredoc
我正在尝试使用 terraform 创建一个复合 cloudwatch 警报。但不幸的是,我的地形代码因以下错误而中断:
Error: error creating CloudWatch Composite Alarm
(node-count-office-time-composite-alarm-DP-1474-desert):
ValidationError: AlarmRule must not contain leading or trailing
whitespace or be null
status code: 400, request 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 版本。任何人都面临类似的问题,我该如何解决这个问题?谢谢
我没有找到如何使 heredoc 工作的解决方案。但我暂时使用直接字符串表达式而不是 heredoc 块修复了它。以下是字符串表达式:
alarm_rule = "ALARM(${aws_cloudwatch_metric_alarm.node_count.alarm_name}) AND ALARM(${aws_cloudwatch_metric_alarm.office_time.alarm_name})"
希望对遇到同样问题的其他人有用。谢谢
截至 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+/", " "))
我正在尝试使用 terraform 创建一个复合 cloudwatch 警报。但不幸的是,我的地形代码因以下错误而中断:
Error: error creating CloudWatch Composite Alarm (node-count-office-time-composite-alarm-DP-1474-desert): ValidationError: AlarmRule must not contain leading or trailing whitespace or be null status code: 400, request 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 版本。任何人都面临类似的问题,我该如何解决这个问题?谢谢
我没有找到如何使 heredoc 工作的解决方案。但我暂时使用直接字符串表达式而不是 heredoc 块修复了它。以下是字符串表达式:
alarm_rule = "ALARM(${aws_cloudwatch_metric_alarm.node_count.alarm_name}) AND ALARM(${aws_cloudwatch_metric_alarm.office_time.alarm_name})"
希望对遇到同样问题的其他人有用。谢谢
截至 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+/", " "))