我想添加一个在Elastic Beanstalk环境创建的应用程序负载均衡器中出现太多5xx错误时触发的警报。
EB环境由terraform脚本创建。我只能在terraform创建资源aws_elastic_beanstalk_environment后知道负载均衡器的名称。
This page说elastic-beanstalk-environment的输出称为elb_load_balancers
。我认为我可能可以使用此输出来创建aws_cloudwatch_metric_alarm资源。
以下Terraform脚本是我现在所做的。它不起作用
resource "aws_cloudwatch_metric_alarm" "alarm_5xx" {
alarm_name = "EB 5XX Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "HTTPCode_ELB_5XX_Count"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Sum"
threshold = "10"
dimensions = {
# How can I put the name of the dynamically generated load balancer here?
LoadBalancer = "${aws_elastic_beanstalk_environment.my_eb_environment_name.elb_load_balancers}" # This line doesn't work
}
alarm_description = "This metric monitors number of 5xx erros in the application load balancer"
}
当我运行terraform apply -target=aws_cloudwatch_metric_alarm.alarm_5xx
时,上面的脚本会产生以下错误:
* aws_cloudwatch_metric_alarm.alarm_5xx: Resource 'aws_elastic_beanstalk_environment.my_eb_environment_name' does not have attribute 'elb_load_balancers' for variable 'aws_elastic_beanstalk_environment.my_eb_environment_name.elb_load_balancers'
我也尝试过
resource "aws_cloudwatch_metric_alarm" "alarm_5xx" {
alarm_name = "EB 5XX Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "HTTPCode_ELB_5XX_Count"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Sum"
threshold = "10"
dimensions = {
LoadBalancer = "${aws_elastic_beanstalk_environment.RightestCARE-Api-Prod-Terraform.load_balancers}" # This line doesn't work
}
alarm_description = "This metric monitors number of 5xx erros in the application load balancer"
}
但这会产生以下错误:
* aws_cloudwatch_metric_alarm.alarm_5xx: dimensions (LoadBalancerName): '' expected type 'string', got unconvertible type '[]interface {}'
答案 0 :(得分:1)
感谢ydaetskcoR
的评论。我发现以下脚本有效。
resource "aws_cloudwatch_metric_alarm" "alarm_5xx" {
alarm_name = "EB 5XX Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "HTTPCode_ELB_5XX_Count"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Sum"
threshold = "10"
dimensions = {
LoadBalancer = "${aws_elastic_beanstalk_environment.my_eb_environment_name.load_balancers[0]}"
}
alarm_description = "This metric monitors number of 5xx erros in the application load balancer"
}
Terraform的aws_elastic_beanstalk_environment说它导出了一个load_balancers属性。
并且由于我在EB环境中只有1个负载均衡器,因此我可以使用load_balancers[0]
获得唯一的负载均衡器。