Terraform:动态路由 table 路由,无法从字符串列表中检索元素
Terraform: Dynamic route table routes, could not retrieve elements from list of string
我在将路由转换为动态路由时遇到了问题。
默认情况下,我有 3 个可用区,每个区都有自己的 nat 网关,当我尝试使用这些时 nat gateways
我收到了不同类型的错误。
这是 nat.tf
(Returns > list[string])
的输出
output "nat_ids" {
description = "The id of created nat gateway"
value = aws_nat_gateway.nat.*.id
}
这是aws_route_table
的区块
resource "aws_route_table" "route" {
count = length(var.subnets)
vpc_id = var.vpc_id
# Route Table For IPv4
dynamic "route" {
for_each = var.route_table_ipv4
content {
cidr_block = lookup(route.value, "cidrblock", "" )
gateway_id = lookup(route.value, "igw", "" )
instance_id = lookup(route.value, "instance", "" )
nat_gateway_id = compact(split(",", lookup(route.value, "nat", "")))
vpc_endpoint_id = lookup(route.value, "vpc_endpoint", "" )
local_gateway_id = lookup(route.value, "local_gateway", "" )
transit_gateway_id = lookup(route.value, "transit_gateway", "" )
network_interface_id = lookup(route.value, "network_interface", "" )
vpc_peering_connection_id = lookup(route.value, "vpc_peering", "" )
}
}
tags = var.map_tags
}
错误信息如下:
Error: Invalid value for module argument
on main.tf line 211, in module "private_routing":
211: route_table_ipv4 = local.route_table_ipv4.private
The given value is not suitable for child module variable "route_table_ipv4"
defined at ../Resources/Network/Routings/variables.tf:107,1-28: element 0:
element "nat": string required.
Error: Incorrect attribute value type
on ../Resources/Network/Routings/main.tf line 28, in resource "aws_route_table" "route":
28: nat_gateway_id = compact(split(",", lookup(route.value, "nat", "")))
Inappropriate value for attribute "nat_gateway_id": string required.
我的问题是什么?我做错了什么?
我尝试了很多选项,例如
element(concat(lookup(route.value, "nat", "" ), [""]), count.index)
element(lookup(route.value, "nat"), count.index)
compact(split(",", lookup(route.value, "nat", "")))
这里是主模块中的变量和声明方式
variable "route_table_ipv4" {
description = "The list of routes for IPv4 'CIDR' block(s)"
type = list(map(string))
default = null
}
. . . other modules . . .
route_table_ipv4 = local.route_table_ipv4.private
. . . other modules . . .
locals{
route_table_ipv4 = {
private = [
{
nat = module.gateways.nat_ids
cidrblock = "0.0.0.0/0"
}
]
}
}
我希望我的post对遇到同样问题的其他人也有帮助,我希望它不会太长,我尽量减少代码。期待您的评论和回答。 internet gateway
没有问题,以防万一你认为我打错了字
我找到了解决方案
1 - 我已将变量的拼写错误更改为 any
2 - 我使用连接到 nat 网关的输出,而不是在我刚刚拆分和计算索引的资源中
我在将路由转换为动态路由时遇到了问题。
默认情况下,我有 3 个可用区,每个区都有自己的 nat 网关,当我尝试使用这些时 nat gateways
我收到了不同类型的错误。
这是 nat.tf
(Returns > list[string])
output "nat_ids" {
description = "The id of created nat gateway"
value = aws_nat_gateway.nat.*.id
}
这是aws_route_table
resource "aws_route_table" "route" {
count = length(var.subnets)
vpc_id = var.vpc_id
# Route Table For IPv4
dynamic "route" {
for_each = var.route_table_ipv4
content {
cidr_block = lookup(route.value, "cidrblock", "" )
gateway_id = lookup(route.value, "igw", "" )
instance_id = lookup(route.value, "instance", "" )
nat_gateway_id = compact(split(",", lookup(route.value, "nat", "")))
vpc_endpoint_id = lookup(route.value, "vpc_endpoint", "" )
local_gateway_id = lookup(route.value, "local_gateway", "" )
transit_gateway_id = lookup(route.value, "transit_gateway", "" )
network_interface_id = lookup(route.value, "network_interface", "" )
vpc_peering_connection_id = lookup(route.value, "vpc_peering", "" )
}
}
tags = var.map_tags
}
错误信息如下:
Error: Invalid value for module argument
on main.tf line 211, in module "private_routing":
211: route_table_ipv4 = local.route_table_ipv4.private
The given value is not suitable for child module variable "route_table_ipv4"
defined at ../Resources/Network/Routings/variables.tf:107,1-28: element 0:
element "nat": string required.
Error: Incorrect attribute value type
on ../Resources/Network/Routings/main.tf line 28, in resource "aws_route_table" "route":
28: nat_gateway_id = compact(split(",", lookup(route.value, "nat", "")))
Inappropriate value for attribute "nat_gateway_id": string required.
我的问题是什么?我做错了什么? 我尝试了很多选项,例如
element(concat(lookup(route.value, "nat", "" ), [""]), count.index)
element(lookup(route.value, "nat"), count.index)
compact(split(",", lookup(route.value, "nat", "")))
这里是主模块中的变量和声明方式
variable "route_table_ipv4" {
description = "The list of routes for IPv4 'CIDR' block(s)"
type = list(map(string))
default = null
}
. . . other modules . . .
route_table_ipv4 = local.route_table_ipv4.private
. . . other modules . . .
locals{
route_table_ipv4 = {
private = [
{
nat = module.gateways.nat_ids
cidrblock = "0.0.0.0/0"
}
]
}
}
我希望我的post对遇到同样问题的其他人也有帮助,我希望它不会太长,我尽量减少代码。期待您的评论和回答。 internet gateway
没有问题,以防万一你认为我打错了字
我找到了解决方案
1 - 我已将变量的拼写错误更改为 any 2 - 我使用连接到 nat 网关的输出,而不是在我刚刚拆分和计算索引的资源中