我为设置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}"
}
答案 0 :(得分:0)
每个环境没有多少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)}"
}
}
这很有趣,但是我可以将任何值传递给element(...,val),并且每次我获得每个环境中唯一正确的ELB名称。