在资源级别使用for / for_each遍历嵌套数据

时间:2019-10-11 14:21:22

标签: terraform

我正在尝试找出如何使用Terraform 0.12.10从以下tfvars文件中给出的复杂对象中迭代嵌套变量的方法:

example.tfvars

virtual_network_data = {
  1 = {
    product_instance_id              = 1
    location                         = "somewhere"
    address_space                    = ["192.168.0.0/23"]
    dns_servers                      = []
    custom_tags                      = {"test":"test value"}
    subnets                          = [
      {
        purpose = "mgmt"
        newbits = 4
        item = 0
      },
      {
        purpose = "transit"
        newbits = 4
        item = 1
      }
    ]
  }
}

example.tf

variable "virtual_network_data" {} #Data comes from example.tfvars

variable "resource_group_name" {
    default = "my_resource_group"
}

variable "virtual_network_name" {
    default = "my_virtual_network"
}

####

resource "azurerm_subnet" "pool" {
    for_each             = var.virtual_network_data

    name                 = format("%s%s%02d", "subnet_", s.purpose, s.item)
    resource_group_name  = var.resource_group_name
    virtual_network_name = var.virtual_network_name
    address_prefix       = cidrsubnet(each.value["address_space"], s.newbits, s.item)

}

example.tf中,我可以使用each.value["address_space"]来获取顶级变量,但是我无法解决如何获取subnetss.purposes.items.newbits)。

我已经使用了动态块作为父资源(如下)的一部分,它可以工作,但是在这种情况下,我需要将子网移动到它自己的资源中。简而言之,如何使第一个for_each的行为像动态块中的第二个for_each一样?

resource "azurerm_virtual_network" "pool" {
  for_each                  = var.virtual_network_data

  name                      = format("%s%02d", local.resource_name, each.key)
  resource_group_name       = var.resource_group_name
  location                  = each.value["location"]
  address_space             = each.value["address_space"]
  dns_servers               = each.value["dns_servers"]
  tags                      = merge(local.tags, each.value["custom_tags"])

  dynamic "subnet" {
    for_each = [for s in each.value["subnets"]: {
      name   = format("%s%s%02d", "subnet_", s.purpose, s.item)
      prefix = cidrsubnet(element(each.value["address_space"],0), s.newbits, s.item)
    }]

    content {
      name            = subnet.value.name
      address_prefix  = subnet.value.prefix
    }
  }
}

厚颜无耻的奖金,有没有办法用s.itemeach.key之类的东西来代替count.index

TIA

1 个答案:

答案 0 :(得分:1)

这种情况下的技术是使用其他Terraform语言功能将您的集合转换为适合for_each参数的形状:每个资源实例一个元素。

对于嵌套数据结构,可以将flatten与两个或多个for表达式结合使用,以产生一个平面数据结构,每个嵌套对象只有一个元素:

locals {
  network_subnets = flatten([
    for network_key, network in var.virtual_network_data : [
      for subnet in network.subnets : {
        network_key       = network_key
        purpose           = subnet.purpose
        parent_cidr_block = network.address_space[0]
        newbits           = subnet.newbits
        item              = subnet.item
      }
    ]
  ])
}

然后,您可以使用local.network_subnets作为重复的基础:

resource "azurerm_subnet" "pool" {
    # Each instance must have a unique key, so we'll construct one
    # by combining the network key, the subnet "purpose", and the "item".
    for_each = {
      for ns in local.network_subnets : "${ns.network_key}.${ns.purpose}${ns.item}" => ns
    }

    name                 = format("%s%s%02d", "subnet_", each.value.purpose, each.value.item)
    resource_group_name  = var.resource_group_name
    virtual_network_name = var.virtual_network_name
    address_prefix       = cidrsubnet(each.value.parent_cidr_block, each.value.newbits, each.value.item)
}

the flatten documentation中有一个类似的示例,作为其他一些上下文。