使用terraform模块向路由表中添加额外路由



我正在使用模块添加路由到路由表。下面是我的代码。它运行成功,但是没有添加路由。

模块。这将检查publicRoute &privateRoute不止一个条目,它会将那么多路由添加到路由表中)

resource "aws_route" "public_routes" {
count   = length(var.ExtraRoutes.publicRoute) > 1 ? length(var.ExtraRoutes.publicRoute) : 0
route_table_id            = aws_route_table.VPCPublicSubnetRouteTable[0].id
destination_cidr_block    = length(regexall("^[0-9].*.[0-9].*",var.ExtraRoutes.publicRoute[count.index].destination)) != 0 ? var.ExtraRoutes.publicRoute[count.index].destination : null
gateway_id = length(regexall("^igw-.*",var.ExtraRoutes.publicRoute[count.index].target)) != 0 ? var.ExtraRoutes.publicRoute[count.index].target : null
}
resource "aws_route" "private_routes" {
count   = length(var.ExtraRoutes.privateRoute) > 1 ? length(var.ExtraRoutes.privateRoute) : 0
route_table_id            = aws_route_table.VPCPrivateSubnetRouteTable[0].id  
destination_cidr_block    = length(regexall("^[0-9].*.[0-9].*",var.ExtraRoutes.privateRoute[count.index].destination)) != 0 ? var.ExtraRoutes.privateRoute[count.index].destination : null  
gateway_id = length(regexall("^igw-.*",var.ExtraRoutes.privateRoute[count.index].target)) != 0 ? var.ExtraRoutes.privateRoute[count.index].target : null
}

module_var。

variable "ExtraRoutes" {
type = map
default = {
publicRoute  = []
privateRoute = []
}
}

主要。因为我需要extrarroutes中的第一项来获取我想要的其他内容。索引+ 1)

module "ExtraVPCs" {
source = "./modules/VPC"
count  =  length(var.ExtraRoutes)
ExtraRoutes = {
publicRoute = var.ExtraRoutes[count.index + 1].publicRoute
privateRoute = var.ExtraRoutes[count.index + 1].privateRoute
}  
}

main_var.tf

variable "ExtraRoutes" {
type = list(object({
publicRoute            = list(object({
destination = string
target = string
})
)
privateRoute = list(object({
destination = string
target = string
}))
}))
}

init。在extrarroutes中有2个条目。它应该在路由表中添加第二项,但它没有像预期的那样工作。

ExtraRoutes = [
{
publicRoute = [
{
destination = "10.0.0.0/32"
target =  "igw-092aba6c187183f48"
}
]
privateRoute = [
{
destination = "10.0.0.0/32"
target =  "igw-092aba6c187183f48"
}
]
},
{
publicRoute = [
{
destination = "10.0.0.0/32"
target =  "igw-0acf4f7ac1e7eba47"
}
]
privateRoute = [
{
destination = "10.0.0.0/32"
target =  "igw-0acf4f7ac1e7eba47"
}
]
}
]

您使用>0而不是>1检查列表的长度:

count   = length(var.ExtraRoutes.publicRoute) > 0 ? length(var.ExtraRoutes.publicRoute) : 0

TF从0中计数物品。当您使用>1时,在您的情况下,您最终使用count = 0

相关内容

  • 没有找到相关文章

最新更新