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

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

Multiple subnets in same azure VNet with Terrafrom

问题

我使用for_each循环创建了1个虚拟网络和3个子网。在创建子网时遇到了错误。我参考了以下URL并尝试解决问题,但无法修复错误。

以下是我的代码。

main.tf

  1. resource "azurerm_virtual_network" "vnet01" {
  2. name = "${var.env}-${var.vnetname}"
  3. location = azurerm_resource_group.RG01Name.location
  4. resource_group_name = azurerm_resource_group.RG01Name.name
  5. address_space = var.vnetaddspace
  6. dns_servers = var.vnetdnsservers
  7. tags = var.tags
  8. }
  9. resource "azurerm_subnet" "subnet" {
  10. for_each = var.subnets
  11. name = each.value.name
  12. address_prefixes = [each.value.address]
  13. resource_group_name = azurerm_resource_group.RG01Name.name
  14. virtual_network_name = azurerm_virtual_network.vnet01.name
  15. }

variable.tf

  1. variable "vnetname" {
  2. type = string
  3. }
  4. variable "vnetaddspace" {
  5. type = list(string)
  6. }
  7. variable "vnetdnsservers" {
  8. type = list(string)
  9. }
  10. variable "subnets" {
  11. type = map(object({
  12. name = string
  13. address = string
  14. }))
  15. }

dev.tfvars

  1. vnetaddspace = ["10.0.0.0/16"]
  2. vnetdnsservers = ["10.0.0.4", "10.0.0.5"]
  3. subnets = {
  4. key1 = { name = "sub01", address = "10.13.1.0/24" }
  5. key2 = { name = "sub02", address = "10.13.2.0/24" }
  6. key3 = { name = "sub03", address = "10.13.3.0/24" }
  7. }

以下是我遇到的错误信息。

  1. Error: Incorrect attribute value type
  2. on NW.tf line 14, in resource "azurerm_subnet" "subnet":
  3. 14: address_prefixes = [each.value.address]
  4. 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

  1. resource "azurerm_virtual_network" "vnet01" {
  2. name = "${var.env}-${var.vnetname}"
  3. location = azurerm_resource_group.RG01Name.location
  4. resource_group_name = azurerm_resource_group.RG01Name.name
  5. address_space = var.vnetaddspace
  6. dns_servers = var.vnetdnsservers
  7. tags = var.tags
  8. }
  9. resource "azurerm_subnet" "subnet" {
  10. for_each = var.subnets
  11. name = each.value.name
  12. address_prefixes = each.value.address
  13. resource_group_name = azurerm_resource_group.RG01Name.name
  14. virtual_network_name = azurerm_virtual_network.vnet01.name
  15. }

variable.tf

  1. variable "vnetname" {
  2. type = string
  3. }
  4. variable "vnetaddspace" {
  5. }
  6. variable "vnetdnsservers" {}
  7. variable "subnets" {
  8. [![enter image description here][2]][2]type = map(object({
  9. name = string
  10. address = string
  11. }))
  12. }

dev.tfvars

  1. vnetaddspace = ["10.0.0.0/16"]
  2. vnetdnsservers = ["10.0.0.4", "10.0.0.5"]
  3. subnets = {
  4. key1 = { name = "sub01", address = "10.13.1.0/24" }
  5. key2 = { name = "sub02", address = "10.13.2.0/24" }
  6. key3 = { name = "sub03", address = "10.13.3.0/24" }
  7. }

Below is the error I am getting

  1. Error: Incorrect attribute value type
  2. on NW.tf line 14, in resource "azurerm_subnet" "subnet":
  3. 14: address_prefixes = each.value.address
  4. ├────────────────
  5. each.value.address is "10.13.1.0/24"
  6. Inappropriate value for attribute "address_prefixes": list of string required.
  7. Error: Incorrect attribute value type
  8. on NW.tf line 14, in resource "azurerm_subnet" "subnet":
  9. 14: address_prefixes = each.value.address
  10. ├────────────────
  11. each.value.address is "10.13.2.0/24"
  12. Inappropriate value for attribute "address_prefixes": list of string required.
  13. Error: Incorrect attribute value type
  14. on NW.tf line 14, in resource "azurerm_subnet" "subnet":
  15. 14: address_prefixes = each.value.address
  16. ├────────────────
  17. each.value.address is "10.13.3.0/24"
  18. Inappropriate value for attribute "address_prefixes": list of string required.

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

答案1

得分: 2

需要的翻译部分:

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

You need a list of strings, not string only:

  1. 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:

确定