在一个资源中查找两个列表

Lookup two lists in one resource

我正尝试在 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

我不相信您的数据源有效,因为它们似乎不正确,因为据我所知您无法通过标签搜索 LB 或 TG。

但无论如何,我试图复制这个问题,我假设每个 NLB 都有一个目标群体,而你的变量lblb_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"
}

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

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

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

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]
    }
  ]
}

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