Amazon web services 在一个资源中查找两个列表

Amazon web services 在一个资源中查找两个列表,amazon-web-services,terraform,cloudwatch-alarms,Amazon Web Services,Terraform,Cloudwatch Alarms,我正在尝试为aws中的NLB为UnHealthyHostCountmetric创建cloudwatch警报 我将NLB定义为: variable "lb" { type = list default = [ "net/lb01/bb087", "net/lb01/bb088" ] } variable "lb_tg" { type = list default = [ "

我正在尝试为aws中的NLB为UnHealthyHostCountmetric创建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"
  ]
}
我将目标群体定义为:

variable "lb" {
  type    = list
  default = [
"net/lb01/bb087",
"net/lb01/bb088"
]
}
variable "lb_tg" {
  type    = list
  default = [
    "targetgroup/newtargetlkinjk/3dac",
    "targetgroup/newtargetlkinjk/3d0d"
  ]
}
然后我在它们上使用datasource,如下所示:

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

由于报警已用于_each=data.aws_lb_target_group.my_lb_target_group,我如何同时向其提供data.aws_lb.my_lbs中的值,这是Dimensions LoadBalancer需要的,因为他们似乎不正确,因为我可以告诉你,你不能通过标签搜索LBs或TGs

但无论如何,我试图复制这个问题,并且我假设每个NLB都有一个目标群体,并且你的变量
lb
lb_-tg
成对匹配,即
nlb1-tg1
nlb2-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
  }))
}
locals {
  target_groups = [
    for i, lb in var.lb : {
      load_balancer = lb
      target_group  = var.lb_tg[i]
    }
  ]
}
因此,在调用者中定义此变量的语法为:

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

如果您有充分的理由需要在输入中将这两个列表分开,您可以在模块中以如下方式将这两个列表组合在一起:

variable "target_groups" {
  type = map(object({
    load_balancer = string
    target_group  = string
  }))
}
locals {
  target_groups = [
    for i, lb in var.lb : {
      load_balancer = lb
      target_group  = var.lb_tg[i]
    }
  ]
}
然后,您可以在我在上面第一个示例中使用
var.target\u组的地方使用
local.target\u组
,效果相同