我想添加一个警报,当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 {}'
感谢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]
来获得唯一的负载均衡器。