【问题标题】:Use a list/tuple in a terraform function using for_each使用 for_each 在 terraform 函数中使用列表/元组
【发布时间】:2021-08-08 02:01:28
【问题描述】:

我有以下来自 terraform locals 的输出。

  [
    [
      "rt1",
      "rg1",
      "10.0.0.0.1/26",
    ],
    [
      "rt1",
      "rg1",
      "10.0.0.32/24",
     ],
     [
      "rt2",
      "rg2",
      "10.0.0.0.1/26",
     ],
     [
      "rt2",
      "rg2",
      "10.0.0.32/24",
     ],
     [
      "rt3",
      "rg3",
      "10.0.0.0.1/26",
     ],
     [
      "rt3",
      "rg3",
      "10.0.0.32/24",
     ],
   ]

下面是函数。由于输出值为 6 个集合/元组 .. 它应该循环 6 次并替换路由表、资源组和子网 cidr 中的 rt、rg 和 ip。我可以通过 count 获得输出。但我想用 for_each 来做这个

resource "azurerm_route" "route" {
 count     = length(local.flattened)
 name                = "test"
 resource_group_name = ((local.flattened[count.index])[1])
 route_table_name    = ((local.flattened[count.index])[0])
 address_prefix      = ((local.flattened[count.index])[2])
 next_hop_type       = "VirtualAppliance"
 next_hop_in_ip_address = "10.220.54.16"            
 }

【问题讨论】:

  • 输出是一个列表,使用for_each它应该是一个地图。如果你能把它做成一张我们可以使用的地图for_each.

标签: terraform


【解决方案1】:

我认为以下应该可行:

resource "azurerm_route" "route" {
   for_each            = {for i,v in local.flattened: i=>v}
   name                = "test"
   resource_group_name = each.value[0]
   route_table_name    = each.value[1]
   address_prefix      = each.value[2]
   next_hop_type       = "VirtualAppliance"
   next_hop_in_ip_address = "10.220.54.16"            
 }

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 2020-06-23
    • 1970-01-01
    • 2020-11-01
    • 2021-07-09
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多