如何使用terraform为Elastic Beanstalk环境创建的负载均衡器添加警报?

问题描述 投票:1回答:1

我想添加一个警报,当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 {}'

amazon-web-services terraform amazon-elastic-beanstalk
1个回答
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]来获得唯一的负载均衡器。

© www.soinside.com 2019 - 2024. All rights reserved.