我想为每个子网和每个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_allocation
值?
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
}
最终解决了这个问题。
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_subnets
中的值时,我使用index()
来获取当前子网的索引值(在本例中为0和1(,以获取var.services.<key>.eip_allocation[]
中的值。