Terraform 带有嵌套地图列表的地形模块计数
我尝试在模块上循环,循环次数等于贴图在嵌套贴图列表中出现的次数,如下所示: vars.tfTerraform 带有嵌套地图列表的地形模块计数,terraform,terraform-provider-aws,terraform-modules,Terraform,Terraform Provider Aws,Terraform Modules,我尝试在模块上循环,循环次数等于贴图在嵌套贴图列表中出现的次数,如下所示: vars.tf variable "http_tcp_listeners" { description = "A list of maps describing the HTTP listeners or TCP ports for this NLB" type = any default = [ { "http_tcp_
variable "http_tcp_listeners" {
description = "A list of maps describing the HTTP listeners or TCP ports for this NLB"
type = any
default = [
{
"http_tcp_listener" = [
{
port = "80"
protocol = "TCP"
},
{
port = "7364"
protocol = "TCP"
}
]
},
{
"http_tcp_listener" = [
{
port = "8080"
protocol = "TCP"
},
{
port = "7365"
protocol = "TCP"
}
]
}
]
}
module "create_network_lb" {
count = length(var."http_tcp_listeners")
source = "../../modules/lb"
subnets = tolist(data.aws_subnet_ids.private_compute[0].ids)
vpc_id = sort(data.aws_vpcs.platform_private_vpc.ids)[0]
target_groups = lookup(var.target_groups[count.index], "target_group", null)
http_tcp_listeners = lookup(var.http_tcp_listeners[count.index], "http_tcp_listener", null)
main.tf
variable "http_tcp_listeners" {
description = "A list of maps describing the HTTP listeners or TCP ports for this NLB"
type = any
default = [
{
"http_tcp_listener" = [
{
port = "80"
protocol = "TCP"
},
{
port = "7364"
protocol = "TCP"
}
]
},
{
"http_tcp_listener" = [
{
port = "8080"
protocol = "TCP"
},
{
port = "7365"
protocol = "TCP"
}
]
}
]
}
module "create_network_lb" {
count = length(var."http_tcp_listeners")
source = "../../modules/lb"
subnets = tolist(data.aws_subnet_ids.private_compute[0].ids)
vpc_id = sort(data.aws_vpcs.platform_private_vpc.ids)[0]
target_groups = lookup(var.target_groups[count.index], "target_group", null)
http_tcp_listeners = lookup(var.http_tcp_listeners[count.index], "http_tcp_listener", null)
模块
resource "aws_lb_listener" "frontend_http_tcp" {
count = var.create_lb ? length(var.http_tcp_listeners) : 0
load_balancer_arn = aws_lb.default[0].arn
port = var.http_tcp_listeners[count.index]["port"]
protocol = var.http_tcp_listeners[count.index]["protocol"]
dynamic "default_action" {
for_each = length(keys(var.http_tcp_listeners[count.index])) == 0 ? [] : [var.http_tcp_listeners[count.index]]
content {
type = lookup(default_action.value, "action_type", "forward")
target_group_arn = contains([null, "", "forward"], lookup(default_action.value, "action_type", "")) ? aws_lb_target_group.main[lookup(default_action.value, "target_group_index", count.index)].id : null
dynamic "redirect" {
for_each = length(keys(lookup(default_action.value, "redirect", {}))) == 0 ? [] : [lookup(default_action.value, "redirect", {})]
content {
path = lookup(redirect.value, "path", null)
host = lookup(redirect.value, "host", null)
port = lookup(redirect.value, "port", null)
protocol = lookup(redirect.value, "protocol", null)
query = lookup(redirect.value, "query", null)
status_code = redirect.value["status_code"]
}
}
dynamic "fixed_response" {
for_each = length(keys(lookup(default_action.value, "fixed_response", {}))) == 0 ? [] : [lookup(default_action.value, "fixed_response", {})]
content {
content_type = fixed_response.value["content_type"]
message_body = lookup(fixed_response.value, "message_body", null)
status_code = lookup(fixed_response.value, "status_code", null)
}
}
}
}
}
执行“地形计划”时,它仅显示最后一个“http\U tcp\U侦听器”值。模块变量的格式必须为“[{port=80,protocol=“TCP”},{port=7364,protocol=“TCP”}]”,因此,每次迭代“http_TCP_listener”之后的所有内容都必须是
在故障排除过程中,Terraform似乎认为变量是一个元组,每个错误有一个元素:
Error: Invalid index
on main.tf line 86, in module "create_network_lb":
86: http_tcp_listeners = [lookup(var.http_tcp_listeners[1], "http_tcp_listener")]
|----------------
| var.http_tcp_listeners is tuple with 1 element
The given key does not identify an element in this collection value.
如果我手动将其中一个键从“http_tcp_listener”更改为“http_tcp_listener1”,并将其反映在main.tf查找值中,它将显示该值。i、 e,如果我重命名第一个键并引用它,terraform plan将显示端口80和7364,而不是8080和7365
任何帮助都将不胜感激。通过重新创建数据结构并使用for_调用模块来解决。详细信息。您是否实际应用了该代码以确认其不起作用?你有任何错误吗?应用代码是可行的,尽管它只支持最后一个“http\U tcp\U listener”映射。我认为这是你的模块出了问题。您能说明如何在模块中定义侦听器吗?如何使用http_tcp_监听器?用“模块”代码片段更新了我的帖子。谢谢。那么
create\u network\u lb
模块创建两个NLB?