gpt4 book ai didi

azure - 具有 Terraform 的同一 azure VNet 中的多个子网

转载 作者:行者123 更新时间:2023-12-03 05:13:41 26 4
gpt4 key购买 nike

我正在使用 for_each 循环创建 1 个虚拟网络和 3 个子网。我在创建子网时遇到错误 I have taken reference from this URL并尝试解决该问题,但无法修复错误。

下面是我的代码。

main.tf

 resource "azurerm_virtual_network" "vnet01" {
name = "${var.env}-${var.vnetname}"
location = azurerm_resource_group.RG01Name.location
resource_group_name = azurerm_resource_group.RG01Name.name
address_space = var.vnetaddspace
dns_servers = var.vnetdnsservers
tags = var.tags
}


resource "azurerm_subnet" "subnet" {
for_each = var.subnets
name = each.value.name
address_prefixes = each.value.address
resource_group_name = azurerm_resource_group.RG01Name.name
virtual_network_name = azurerm_virtual_network.vnet01.name
}

变量.tf

    variable "vnetname" {
type = string
}
variable "vnetaddspace" {
}
variable "vnetdnsservers" {}

variable "subnets" {
[![enter image description here][2]][2]type = map(object({
name = string
address = string
}))
}

dev.tfvars

vnetaddspace   = ["10.0.0.0/16"]
vnetdnsservers = ["10.0.0.4", "10.0.0.5"]

subnets = {
key1 = { name = "sub01", address = "10.13.1.0/24" }
key2 = { name = "sub02", address = "10.13.2.0/24" }
key3 = { name = "sub03", address = "10.13.3.0/24" }
}

下面是我收到的错误

│ Error: Incorrect attribute value type

│ on NW.tf line 14, in resource "azurerm_subnet" "subnet":
│ 14: address_prefixes = each.value.address
│ ├────────────────
│ │ each.value.address is "10.13.1.0/24"

│ Inappropriate value for attribute "address_prefixes": list of string required.


│ Error: Incorrect attribute value type

│ on NW.tf line 14, in resource "azurerm_subnet" "subnet":
│ 14: address_prefixes = each.value.address
│ ├────────────────
│ │ each.value.address is "10.13.2.0/24"

│ Inappropriate value for attribute "address_prefixes": list of string required.


│ Error: Incorrect attribute value type

│ on NW.tf line 14, in resource "azurerm_subnet" "subnet":
│ 14: address_prefixes = each.value.address
│ ├────────────────
│ │ each.value.address is "10.13.3.0/24"

│ Inappropriate value for attribute "address_prefixes": list of string required.

enter image description here

最佳答案

您需要一个字符串列表,而不仅仅是字符串:

address_prefixes     = [each.value.address]

关于azure - 具有 Terraform 的同一 azure VNet 中的多个子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75599185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com