【问题标题】:How to use terraform to add an alarm to the load balancer created by Elastic Beanstalk environment?如何使用 terraform 向 Elastic Beanstalk 环境创建的负载均衡器添加警报?
【发布时间】:2019-09-02 23:19:37
【问题描述】:

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

【问题讨论】:

  • aws_elastic_beanstalk_environment 资源的输出只是 load_balancers

标签: amazon-web-services terraform amazon-elastic-beanstalk


【解决方案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] 来获取唯一的负载均衡器。

【讨论】:

  • 您的答案是错误的,因为您再次使用elb_load_balancers 而不是load_balancers 作为输出属性。
  • 感谢提醒。我将elb_load_balancers 更改为load_balancers
猜你喜欢
  • 2019-10-25
  • 1970-01-01
  • 2014-02-11
  • 2017-06-01
  • 2019-04-21
  • 2016-12-13
  • 2019-01-24
  • 2017-02-14
  • 2013-08-10
相关资源
最近更新 更多