【问题标题】:Adding Extra Routes to route table using terraform module使用 terraform 模块向路由表添加额外路由
【发布时间】:2021-10-30 15:05:09
【问题描述】:

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

module.tf: (如果publicRoute和privateRoute有多个item,会添加那么多路由到路由表)

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.tf(我只保留了一张地图)

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

main.tf(因为我需要 ExtraRoutes 中的第一项来获取 count.index + 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.tfvars(ExtraRoutes 中有 2 个项目。它应该在 Route 表中添加第 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"
      }
      ]
}
]

【问题讨论】:

  • “未按预期工作”并不具体。究竟发生了什么,以及预期的结果究竟应该是什么。
  • 它应该将路由添加到路由表中。相反,这项工作是成功的,但没有添加任何内容,这让我认为它可能没有获得任何价值,但没有错误。 :(

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

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

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

TF 计算来自0 的项目。当您使用>1 时,您最终会使用count = 0

【讨论】:

  • 我认为它与module_var.tf有关。这给出了错误:目的地:需要字符串。
  • @MayaRay 这是var.ExtraRoutes.publicRoute[count.index].target 不正确,不是因为“需要字符串”。我认为您在 SO 中发布的内容实际上并不代表您的代码。错误,或者说缺少错误,很奇怪。
  • 我的意思是代码的行为通常与 module_var.tf 有关。我认为我在模块和子项中定义变量 ExtraRoutes 的方式不正确。我提到的错误是我使用您的解决方案后得到的。
  • 我认为这也行不通,因为输入数据不像他们试图那样容易解析。如果您查看var.ExtraRoutes,它是一个列表,因此您无法像var.ExtraRoutes.publicRoute 那样访问它。我认为这需要进行更广泛的返工才能可行,并且可能需要更改输入数据结构。
  • @ydaetskcoR 谢谢。我也一样,但var.ExtraRoutes是一张地图。有两个变量称为ExtraRoutes。一个在主要(列表)中,一个在模块中(地图)。这个问题非常令人困惑。
猜你喜欢
  • 2012-02-23
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 2010-12-16
  • 2021-12-02
  • 2018-10-23
  • 2018-01-22
  • 1970-01-01
相关资源
最近更新 更多