【问题标题】:CloudWatch metric alarm using Terraform使用 Terraform 的 CloudWatch 指标警报
【发布时间】:2019-12-26 20:08:31
【问题描述】:

由于某种原因尝试使用 Terraform 设置一些 CloudWatch 警报时,它找不到指标并且警报仍然卡在数据不足的情况下。 Terraform 不会输出任何错误,如果我在 AWS 中手动搜索,我可以找到指标。我在这里错过了什么?

一个简单的健康主机警报指向目标组的例子:

#healthy host alarm
resource "aws_cloudwatch_metric_alarm" "health" {
  alarm_name          = "${var.tag_app}_healthy_host"
  comparison_operator = "LessThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "HealthyHostCount"
  namespace           = "AWS/ApplicationELB"
  period              = "60"
  statistic           = "Maximum"
  threshold           = "1"
  alarm_description   = "Healthy host count for EC2 machine"
  alarm_actions       = ["${data.aws_sns_topic.blabla.arn}"]
  ok_actions          = ["${data.aws_sns_topic.blabla.arn}"]

  dimensions = {
    TargetGroup  = "${aws_lb_target_group.alb_target.arn_suffix}"
  }
}

当我选择另一个资源(EC2、RDS)和另一个指标时,我会收到指向正确指标的 CloudWatch 警报,并且不会因为数据不足而卡住。

【问题讨论】:

    标签: amazon-web-services terraform amazon-cloudwatch terraform-provider-aws


    【解决方案1】:

    HealthyHostCount metric 仅在TargetGroup, LoadBalancer 维度或TargetGroup, AvailabilityZone, LoadBalancer 下可用,因此您至少还需要添加LoadBalancer 维度才能访问此指标。

    所以你的 Terraform 代码应该是:

    #healthy host alarm
    resource "aws_cloudwatch_metric_alarm" "health" {
      alarm_name          = "${var.tag_app}_healthy_host"
      comparison_operator = "LessThanThreshold"
      evaluation_periods  = "1"
      metric_name         = "HealthyHostCount"
      namespace           = "AWS/ApplicationELB"
      period              = "60"
      statistic           = "Maximum"
      threshold           = "1"
      alarm_description   = "Healthy host count for EC2 machine"
      alarm_actions       = ["${data.aws_sns_topic.blabla.arn}"]
      ok_actions          = ["${data.aws_sns_topic.blabla.arn}"]
    
      dimensions = {
        LoadBalancer = "${aws_lb.example.arn_suffix}"
        TargetGroup  = "${aws_lb_target_group.alb_target.arn_suffix}"
      }
    }
    

    【讨论】:

    • 你怎么知道哪些指标需要哪些维度?
    • 这真的应该作为一个单独的问题来问。
    猜你喜欢
    • 2019-05-04
    • 2021-05-25
    • 2019-07-14
    • 2019-08-17
    • 2021-10-30
    • 2023-03-06
    • 2020-09-13
    • 2020-03-11
    • 2022-01-21
    相关资源
    最近更新 更多