首页 > 解决方案 > 如何在 terraform 中组合 2 个本地变量?

问题描述

我想组合 2 个本地变量,因为我需要将信息传递给负载均衡器目标组附件资源

locals {
    test-svc = {for idx,svc in local.merged_lbport_svc : "${svc.service}-${svc.port}" => svc ...}
  }

locals {
  service_instance_map = merge([for env, value in data.aws_instances.ecom-instances:
                  {
                    for id in value.ids:
                    "${env}-${id}" => {
                      "service" = env
                      "id" = id
                    }
                  }
                ]...)
}

2个当地人的输出如下

locals-service-instance-map = {
  "3ds-i-038ff8f67582ef64d" = {
    "id" = "i-038ff8f67582ef64d"
    "service" = "test"
  }
  "3ds-i-095f20e434879e241" = {
    "id" = "i-095f20e434879e241"
    "service" = "test"
  }
  "3ds-i-0d5692eacef255831" = {
    "id" = "i-0d5692eacef255831"
    "service" = "test"
  }
  "jsclient-i-0b30e97ca40f4359e" = {
    "id" = "i-0b30e97ca40f4359e"
    "service" = "jsc"
  }
  "jsclient-i-0e5fe6123c371d75d" = {
    "id" = "i-0e5fe6123c371d75d"
    "service" = "jsc"
  }
  "jsclient-i-0f108c650773bbc08" = {
    "id" = "i-0f108c650773bbc08"
    "service" = "jsc"
  }
  "validation-i-00d70dcd4fc72fff4" = {
    "id" = "i-00d70dcd4fc72fff4"
    "service" = "valid"
  }
  "validation-i-05cc0d238006cfe56" = {
    "id" = "i-05cc0d238006cfe56"
    "service" = "valid"
  }
  "validation-i-0ad6c30d56b0be26b" = {
    "id" = "i-0ad6c30d56b0be26b"
    "service" = "valid"
  }
}
locals-test-svc = {
  "test-443" = [
    {
      "port" = 443
      "protocol" = "TCP"
      "service" = "test"
    },
  ]
  "test-80" = [
    {
      "port" = 80
      "protocol" = "TCP"
      "service" = "test"
    },
  ]
  "jsc-443" = [
    {
      "port" = 443
      "protocol" = "TCP"
      "service" = "jsc"
    },
  ]
  "jsc-80" = [
    {
      "port" = 80
      "protocol" = "TCP"
      "service" = "jsc"
    },
  ]
  "valid-443" = [
    {
      "port" = 443
      "protocol" = "TCP"
      "service" = "valid"
    },
  ]
  "valid-80" = [
    {
      "port" = 80
      "protocol" = "TCP"
      "service" = "valid"
    },
  ]
}

现在我想将这 2 个本地人和我的结果变量结合起来,如下所示

"test-i-038ff8f67582ef64d" = {
    "id" = "i-038ff8f67582ef64d"
    "service" = "test"
    "port" = 80
    "protocol" = "TCP"
  }
  "test-i-095f20e434879e241" = {
    "id" = "i-095f20e434879e241"
    "service" = "test"
    "port" = 80
    "protocol" = "TCP"
  }
  "test-i-0d5692eacef255831" = {
    "id" = "i-0d5692eacef255831"
    "service" = "test"
    "port" = 80
    "protocol" = "TCP"
  }
  "jsc-i-0b30e97ca40f4359e" = {
    "id" = "i-0b30e97ca40f4359e"
    "service" = "jsc"
    "port" = 80
    "protocol" = "TCP"
  }
  "jsc-i-0e5fe6123c371d75d" = {
    "id" = "i-0e5fe6123c371d75d"
    "service" = "jsc"
    "port" = 80
    "protocol" = "TCP"
  }
  "jsc-i-0f108c650773bbc08" = {
    "id" = "i-0f108c650773bbc08"
    "service" = "jsc"
    "port" = 80
    "protocol" = "TCP"
  }
  "valid-i-00d70dcd4fc72fff4" = {
    "id" = "i-00d70dcd4fc72fff4"
    "service" = "valid"
    "port" = 80
    "protocol" = "TCP"
  }
  "valid-i-05cc0d238006cfe56" = {
    "id" = "i-05cc0d238006cfe56"
    "service" = "valid"
    "port" = 80
    "protocol" = "TCP"
  }
  "valid-i-0ad6c30d56b0be26b" = {
    "id" = "i-0ad6c30d56b0be26b"
    "service" = "valid"
    "port" = 80
    "protocol" = "TCP"
  }
}

端口 443 和 TCP 协议也需要相同的组合。然后我将把它传递给 for_each 循环中的负载均衡器目标组附件资源

resource "aws_lb_target_group_attachment" "ecom-tga" {
   for_each          = local.service_instance_map
   target_group_arn  = aws_lb_target_group.ecom-nlb-tgp["${each.value.service}-${each.value.port}"].arn
   port              = each.value.port
   target_id         = each.value.id

   depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}

截至目前,我正在对端口值进行 hartcoding,如下所示

resource "aws_lb_target_group_attachment" "ecom-tga80" {
   for_each          = local.service_instance_map
   #for_each          = null_resource.maps
   target_group_arn  = aws_lb_target_group.ecom-nlb-tgp["${each.value.service-name}-80"].arn
   #port              = aws_lb_target_group.ecom-nlb-tgp[each.value.service-name["port"]]
   target_id         = each.value.id
  # depends_on = [
  #   aws_instance.ecom-validation-service
  # ]
   depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}

resource "aws_lb_target_group_attachment" "ecom-tga443" {
   for_each          = local.service_instance_map
   #for_each          = null_resource.maps
   target_group_arn  = aws_lb_target_group.ecom-nlb-tgp["${each.value.service-name}-443-TCP"].arn
   #port              = aws_lb_target_group.ecom-nlb-tgp[each.value.service-name["port"]]
   target_id         = each.value.id
  # depends_on = [
  #   aws_instance.ecom-validation-service
  # ]
   depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}

即使我们有任何其他更好的想法,请建议我

标签: terraformterraform-provider-aws

解决方案


推荐阅读