Terraform 如何从变量将现有EIP分配附加到NLB

Terraform 如何从变量将现有EIP分配附加到NLB,terraform,terraform-provider-aws,terraform0.12+,Terraform,Terraform Provider Aws,Terraform0.12+,我想为每个子网和每个NLB分配一组预定义的EIP。我有以下变量: swarm_subnets = [ "subnet-xxxx", "subnet-yyyy" ] services = { "service1" = { name = "service1" port = "30081" eip_allocation = [ "eipall

我想为每个子网和每个NLB分配一组预定义的EIP。我有以下变量:

swarm_subnets = [
  "subnet-xxxx",
  "subnet-yyyy"
]
services = {
  "service1" = {
    name = "service1"
    port = "30081"
    eip_allocation = [
        "eipalloc-xxxxxx",
        "eipalloc-xxxxxx"
    ]
  },
  "service2" = {
    name = "service2"
    port = "8445"
    eip_allocation = [
        "eipalloc-xxxxxx",
        "eipalloc-xxxxxx"
    ]
  },
  "service3" = {
    name = "service3"
    port = "8444"
    eip_allocation = [
        "eipalloc-xxxxxx",
        "eipalloc-xxxxxx"
    ]
  }
}
如何使用aws_lb资源内动态块中的
eip_分配

resource "aws_lb" "this" {
    for_each = var.services

    name               = "nlb-${each.key}-${var.environment}"
    internal           = false
    load_balancer_type = "network"
    dynamic "subnet_mapping" {
        for_each = var.swarm_subnets
        content {
            subnet_id = subnet_mapping.value
            allocation_id = <eip_allocation from the services variable>
        }
    }
    tags = local.common_tags
}
资源“aws_lb”“this”{
for_each=var.services
name=“nlb-${each.key}-${var.environment}”
内部=错误
负载均衡器类型=“网络”
动态“子网映射”{
对于每个=变量群集子网
内容{
子网\u id=子网\u映射.value
分配\u id=
}
}
标记=本地.通用\u标记
}

最终找到了答案

resource "aws_lb" "this" {
  for_each = var.services

  name               = "nlb-${each.key}-${var.environment}"
  internal           = false
  load_balancer_type = "network"
  dynamic "subnet_mapping" {
    for_each = var.swarm_subnets
    content {
      subnet_id     = subnet_mapping.value
      allocation_id = each.value.eip_allocation[index(var.swarm_subnets, subnet_mapping.value)]
    }
  }
  tags = local.common_tags
}

当它在
var.swarm\u子网
中循环时,我曾经获取当前子网的索引值(本例中为0和1),以获取
var.services..eip\u分配[]

中的值。看到了吗?谢谢!我从你分享的链接中得到了一些想法。