【问题标题】:Lookup two lists in one resource在一个资源中查找两个列表
【发布时间】:2021-04-10 13:25:36
【问题描述】:

我正在尝试在 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所需要的

【问题讨论】:

    标签: amazon-web-services terraform cloudwatch-alarms


    【解决方案1】:

    我不相信您的数据源有效,因为它们似乎不正确,因为据我所知,您无法按标签搜索 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"
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-08
        • 2021-05-10
        • 2011-01-25
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        相关资源
        最近更新 更多