多个子网在同一个Azure虚拟网络中,使用Terraform。

huangapple go评论65阅读模式
英文:

Multiple subnets in same azure VNet with Terrafrom

问题

我使用for_each循环创建了1个虚拟网络和3个子网。在创建子网时遇到了错误。我参考了以下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
}

variable.tf

variable "vnetname" {
  type = string
}

variable "vnetaddspace" {
  type = list(string)
}

variable "vnetdnsservers" {
  type = list(string)
}

variable "subnets" {
  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]

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

希望这能帮助您解决问题。如果您需要更多帮助,请随时提出。

英文:

I am creating 1 virtual network and 3 subnets using for_each loop.
I am geting error in creating subnets
I have taken reference from this URL and tried to troubleshoot the issue but unable to fix the error.

Below is my code.

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
}

variable.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" }
}

Below is the error I am getting

│ 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.

多个子网在同一个Azure虚拟网络中,使用Terraform。

答案1

得分: 2

需要的翻译部分:

address_prefixes     = [each.value.address]
英文:

You need a list of strings, not string only:

address_prefixes     = [each.value.address]

huangapple
  • 本文由 发表于 2023年3月1日 10:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定