0

我正在尝试在 aws 中为 UnHealthyHostCountmetric 的 NLB 创建 cloudwatch 警报

我将 NLB 定义为:

variable "lb" {
  type    = list
  default = [
"net/lb01/bb087",
"net/lb01/bb088"
]
}

我将目标群体定义为:

variable "lb_tg" {
  type    = list
  default = [
    "targetgroup/newtargetlkinjk/3dac",
    "targetgroup/newtargetlkinjk/3d0d"
  ]
}

然后我在它们上使用数据源:

data "aws_lb_target_group" "my_lb_target_group" {

  for_each = toset(var.lb_tg)

  tags = {
    name = each.key
  }
}

data "aws_lb" "my_lbs" {

  for_each = toset(var.lb)

  tags = {
    name = each.key
  }
}

然后我试图在警报中使用两者

resource "aws_cloudwatch_metric_alarm" "nlb-target-unhealthy-warning" {

  for_each = data.aws_lb_target_group.my_lb_target_group

  alarm_name          = "nlb-target-unhealthy-warning-for-${each.key}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "3"
  metric_name         = "UnHealthyHostCount"
  namespace           = "AWS/NetworkELB"
  dimensions = {
    TargetGroup  = each.key
    LoadBalancer = ???
  }
  period                    = "60"
  statistic                 = "Average"
  threshold                 = "0"
  alarm_description         = "This warning metric monitors unhealthy hosts behind the NLB for ${each.key}"
  actions_enabled           = true
  alarm_actions             = [data.aws_sns_topic.my_sns.arn]
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"
}

由于警报已经在使用 for_each = data.aws_lb_target_group.my_lb_target_group ,我如何同时提供 data.aws_lb.my_lbs 中的值,这是dimentions-LoadBalancer所需要的

4

2 回答 2

1

我不相信您的数据源有效,因为它们似乎不正确,因为您无法根据我的判断按标签搜索 LB 或 TG。

但无论如何,我试图复制这个问题,我假设每个 NLB 都有一个目标组和你的变量lb,并且成对lb_tg匹配,即, 。nlb1 - tg1nlb2 - tg2

在这种情况下,您可以使用以下命令创建警报count

resource "aws_cloudwatch_metric_alarm" "nlb-target-unhealthy-warning" {

  count               =  length(var.lb)

  alarm_name          = "nlb-target-unhealthy-warning-for-${var.lb_tg[count.index]}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "3"
  metric_name         = "UnHealthyHostCount"
  namespace           = "AWS/NetworkELB"  
   
  dimensions = {
    TargetGroup  = data.aws_lb_target_group.my_lb_target_group[var.lb_tg[count.index]].arn_suffix
    LoadBalancer = data.aws_lb.my_lbs[var.lb[count.index]].arn_suffix
  }  
  
  period                    = "60"
  statistic                 = "Average"
  threshold                 = "0"
  alarm_description         = "This warning metric monitors unhealthy hosts behind the NLB for ${var.lb_tg[count.index]}"
  actions_enabled           = true
  alarm_actions             = [data.aws_sns_topic.my_sns.arn]
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"
}
于 2021-01-04T23:50:35.877 回答
0

鉴于这些负载均衡器和目标组对是相互关联的,我建议将它们表示为单个变量,以便它们之间的相关性更加明确,如下所示:

variable "target_groups" {
  type = map(object({
    load_balancer = string
    target_group  = string
  }))
}

因此,在调用者中定义此变量的语法为:

  target_groups = {
    lb01 = {
      load_balancer = "net/lb01/bb087"
      target_group  = "targetgroup/newtargetlkinjk/3dac"
    }
    lb02 = {
      load_balancer = "net/lb01/bb088"
      target_group  = "targetgroup/newtargetlkinjk/3d0d"
    }
  }

除了让未来的读者更容易查看哪些负载均衡器对应于哪些目标组之外,这还提供了一个用于在模块内将它们关联起来的键。

resource "aws_cloudwatch_metric_alarm" "nlb-target-unhealthy-warning" {
  for_each = var.target_groups

  alarm_name          = "nlb-target-unhealthy-warning-for-${each.key}"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "3"
  metric_name         = "UnHealthyHostCount"
  namespace           = "AWS/NetworkELB"
  dimensions = {
    TargetGroup  = each.value.target_group
    LoadBalancer = each.value.load_balancer
  }
  period                    = "60"
  statistic                 = "Average"
  threshold                 = "0"
  alarm_description         = "This warning metric monitors unhealthy hosts behind the NLB for ${each.key}"
  actions_enabled           = true
  alarm_actions             = [data.aws_sns_topic.my_sns.arn]
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"
}

如果您有充分的理由需要在输入中将这两个列表分开,您可以在模块中将这两个列表组合在一起,形成一个本地值,如下所示:

locals {
  target_groups = [
    for i, lb in var.lb : {
      load_balancer = lb
      target_group  = var.lb_tg[i]
    }
  ]
}

然后,您可以在我上面第一个示例中使用local.target_groups的地方使用var.target_groups,效果相同。

于 2021-01-06T01:17:20.947 回答