【问题标题】:create the multiple vnet and associated multiple subnet with list of list of object using terraform使用 terraform 使用对象列表创建多个 vnet 和关联的多个子网
【发布时间】:2020-12-18 07:32:36
【问题描述】:

在这里,我正在尝试创建多个 vnet 并将多个子网与对象列表列表关联

我收到以下错误

此默认值与变量的类型约束不兼容: 元素 0:属性“vnet_name”:需要字符串。


variable networks {
 type = list(object({
    vnet_name           = string
    address_space       = list(string)
    rg_name             = string
    location            = string
    subnet  = list(object({
        sunet_name      = string
        address_prefixes = string
    }))    
  }))
    default = [{
    vnet_name           = "vnet-app"
    address_space       = ["10.0.0.0/16"]
    rg_name             = "TEST-RG"
    location            = "West US"
    subnet              =[{
        subnet_name="snet-vnet"
        address_prefixes ="10.0.0.0/24"
    }]
    }]
}

resource "azurerm_virtual_network" "vnet" {
  for_each = var.networks
  name                = each.value.vnet_name
  address_space       = each.value.address_space
  location            = each.value.location
  resource_group_name = each.value.rg_name
}

【问题讨论】:

    标签: terraform


    【解决方案1】:

    拼写错误。

    代替

    sunet_name      = string
    

    应该是

    subnet_name      = string
    

    【讨论】:

      【解决方案2】:

      for_each 不接受列表。您需要使用 set 或 map 或切换到 count 而不是 for_each 例如

      resource "azurerm_virtual_network" "vnet" {
        count = length(var.networks)
        name                = var.networks[count.index].vnet_name
        address_space       = var.networks[count.index].address_space
        location            = var.networks[count.index].location
        resource_group_name = var.networks[count.index].rg_name
      }
      

      【讨论】:

      • 我是 terraform 新手,如何在同一个 vnet 块中创建子网
      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 2019-11-22
      • 2022-09-28
      • 1970-01-01
      • 2020-11-30
      • 2021-08-09
      • 2019-12-31
      • 2021-02-25
      相关资源
      最近更新 更多