使用 Terraform 的 CloudWatch 指标警报 [英] CloudWatch metric alarm using Terraform

查看:51
本文介绍了使用 Terraform 的 CloudWatch 指标警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When trying to setup some CloudWatch alarms using Terraform for some reason it doesn't find the metrics and the alarm remains stuck in insufficient data. Terraform doesn't output any errors and I can find the metrics if I search manually in AWS. What am I missing here?

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

Example a simple healthy host alarm point to a target group:

#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 警报,并且不会因为数据不足而卡住.

When I select another resource (EC2, RDS) and another metric I get a CloudWatch alarm pointing to the right metric and it doesn't remain stuck at insufficient data.

推荐答案

HealthyHostCount 指标 仅适用于 TargetGroup, LoadBalancer 维度或 TargetGroup, AvailabilityZone, LoadBalancer 所以您至少还需要添加 LoadBalancer 维度才能访问此指标.

The HealthyHostCount metric is only available under either the TargetGroup, LoadBalancer dimensions or the TargetGroup, AvailabilityZone, LoadBalancer so you need to at least add the LoadBalancer dimension as well to access this metric.

所以你的 Terraform 代码应该是:

So your Terraform code should instead be:

#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}"
  }
}

这篇关于使用 Terraform 的 CloudWatch 指标警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆