【问题标题】:how to attach multiple instances with multiple resources in terraform?如何在 terraform 中附加具有多个资源的多个实例?
【发布时间】:2021-07-29 11:23:51
【问题描述】:

我有 3 个名为 valid、test、jsc 的服务,每个都有 3 个 ec2 实例,每个都有 1 个负载均衡器目标组。现在我想将每个 3 个相应的 ec2 实例附加到它们的目标组

variable "service-names" {
  type = list
  default = ["valid","jsc","test"]
  
}

locals {
  helper_map = {for idx, val in setproduct(var.service-names, range(var.instance_count)): 
                   idx => {service_name = val[0]}
               }
}

resource "aws_lb_target_group" "ecom-tgp" {
  for_each = toset(var.service-names)
  name = "${each.value}-tgp"
  port = 80
  protocol = "TCP"
  vpc_id = aws_vpc.ecom-vpc.id
  target_type = "instance"
  deregistration_delay    = 90
  health_check {
    interval            = 30
    port                = 80
    protocol            = "TCP"
    healthy_threshold   = 3
    unhealthy_threshold = 3
  }
  tags = {
    "Name" = "${each.value}-tgp"
  }
  
}

output "ecom-instance-details" {
  value = data.aws_instances.ecom-instances
}

以下是来自 terraform 输出的示例 i 服务实例详细信息

ecom-instance-details = {
  "test" = {
    "filter" = toset(null) /* of object */
    "id" = "ap-south-1"
    "ids" = tolist([
      "i-0fab28125d684f9d2",
      "i-0b90e3501715681df",
      "i-066bff51352660006",
    ])
    "instance_state_names" = toset([
      "running",
      "stopped",
    ])
    "instance_tags" = tomap({
      "Name" = "test-service"
    })
    "private_ips" = tolist([
      "10.0.3.11",
      "10.0.3.10",
      "10.0.3.8",
    ])
    "public_ips" = tolist([])
  }
vpc-id = "vpc-051198b7ebaa3bd53"

我正在尝试如下。但由于它在一个列表中有 3 个 id,它不接受传递给 target_id 这基本上是一个字符串

 resource "aws_lb_target_group_attachment" "ecom-tga" {
   for_each      = local.helper_map
     target_group_arn  = aws_lb_target_group.ecom-tgp[each.value.service_name].arn
     port              = 80
   target_id           = data.aws_instances.ecom-instances[each.value.service_name].ids
 }

你能指导我吗

【问题讨论】:

  • 当前代码有什么问题?有什么错误吗?
  • 错误:属性值类型不正确││在 main.tf 第 500 行,资源“aws_lb_target_group_attachment”“ecom-tga”中:│500:target_id = data.aws_instances.ecom-instances[each.value .service_name].ids │ ├────────────── │ │ data.aws_instances.ecom-instances 是具有 3 个属性的对象
  • 因为有 3 个 id 无法传递每个 id。需要一些循环

标签: amazon-web-services terraform


【解决方案1】:

如果我理解正确,您可以构造一个辅助局部变量并使用它:

locals {

  env_instance_map = merge([for env, value in var.ecom-instance-details:
                  {
                    for id in value.ids:
                    "${env}-${id}" => {
                      "env" = env
                      "id" = id
                    }
                  }
                ]...)
}

resource "aws_lb_target_group_attachment" "ecom-tga" {
   for_each          = local.env_instance_map
   target_group_arn  = aws_lb_target_group.ecom-tgp[each.value.env].arn
   port              = 80
   target_id         = each.value.id
}

【讨论】:

  • awesome marcin.worked like charm.Just 将 var 更改为数据源
  • @user10912187 没问题。很高兴它解决了:-)
  • 不幸的是,由于 - 517:for_each = local.service_instance_map │ ├──────────────── │ │ local .service_instance_map 只有在 apply 之后才能知道 │“for_each”值取决于资源属性,直到 apply 才能确定,因此 Terraform 无法预测将创建多少个实例。要解决此问题,请使用 -target 参数首先仅应用 for_each 所依赖的资源
  • @user10912187 遗憾的是,我不知道您的完整设置是什么。在您的问题中,您似乎预先了解了整个数据结构。
猜你喜欢
  • 2023-01-07
  • 1970-01-01
  • 2020-01-26
  • 2018-07-09
  • 2021-08-09
  • 1970-01-01
  • 2021-06-29
  • 2021-01-12
  • 2020-12-08
相关资源
最近更新 更多