我正在为设置ELB的Elastic Beanstalk应用程序设置Terraform配置。我们想要一个在ELB获得太多5XX错误时触发的云监视器警报。我正在尝试从EB环境中传递ELB ARN,但它失败并显示以下消息:
value of 'count' cannot be computed
我知道这是Terraform的一个常见问题,例如https://github.com/hashicorp/terraform/issues/10857我无法找到解决方法。我们正在努力使这款ELB云观察模块通用,因此我无法真正硬编码ELB的数量。
这是我正在使用的代码:
locals {
elb_count = "${length(var.load_balancer_arns)}"
}
resource aws_cloudwatch_metric_alarm "panic_time" {
count = "${local.elb_count}"
alarm_name = "${var.application_name}-panic-time"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "HTTPCode_ELB_5XX_Count"
namespace = "AWS/ELB"
period = "60"
statistic = "Sum"
threshold = "${var.max_5xx_errors}"
dimensions {
LoadBalancer = "${element(var.load_balancer_arns, count.index)}"
}
alarm_description = "SNS if we start getting a lot of 500 errors"
alarm_actions = ["${aws_sns_topic.panic_time.arn}"]
}
resource aws_sns_topic "panic_time" {
name = "${var.application_name}-panic-time"
}
resource aws_sns_topic_policy "panic_time" {
arn = "${aws_sns_topic.panic_time.arn}"
policy = "${data.aws_iam_policy_document.panic_time_sns.json}"
}
data aws_iam_policy_document "panic_time_sns" {
statement {
actions = [
"SNS:Publish",
]
resources = [
"${aws_sns_topic.panic_time.arn}",
]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
我从main.tf中的环境传递负载均衡器:
load_balancer_arns = "${module.environment.load_balancers}"
(load_balancers输出看起来像这样:)
output load_balancers {
value = "${aws_elastic_beanstalk_environment.main.load_balancers}"
}
没有得到每个环境有多少个ELB。我为每个环境使用了一个ELB的解决方法:
resource "aws_elastic_beanstalk_environment" "some-environment" {
count = "${length(var.environments)}"
...
}
resource "aws_cloudwatch_metric_alarm" "alarm-unhealthy-host-count" {
count = "${length(var.environments)}"
...
dimensions {
LoadBalancerName = "${element(aws_elastic_beanstalk_environment.some-environment.*.load_balancers[count.index], 0)}"
}
}
这很有趣,但我可以将任何值传递给元素(...,val),并且每次我获得每个环境唯一正确的ELB名称。