Terraform Azure 虚拟网络创建

Terraform azure virtual network creation

我已经编写了以下用于在 terraform azure 中创建虚拟网络的代码: 我在 main.tf、variables.tf 和 terraform.tfvars 的根文件夹中有文件。我有资源组和虚拟网络两个模块。

在根文件夹中,我有以下代码: main.tf

#Resource Group Creation
module "resource_group" {
  source     = "./modules/resource_group"
  rgname_var = var.resource_group
  loc_var    = var.location

}
#Virtual Network Creation
module "vnet" {
  source                = "./modules/vnet"
  loc                   = module.resource_group.loc_o
  rgname                = module.resource_group.rgname_o
address_space       = var.vnet_address_space

terraform.tfvars:

vnet_name             = "VirtualNetwork"
vnet_address_space    = "<ip>"
subnet1_address_prefix="<ip1>"
subnet2_address_prefix="<ip2>"
subnet3_address_prefix="<ip3">
在 vnet 模块中,我有以下代码:

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
 location            = var.loc
 resource_group_name = var.rgname
 address_space       = var.vnet_address_space

} 
#Subnet Creation
resource "azurerm_subnet" "subnet-1" {
  name= var.subnet_name1
   resource_group_name  = var.rgname
   virtual_network_name = azurerm_virtual_network.vnet.name
   address_prefix       = var.subnet1_address_prefix   
}
#Subnet Creation
resource "azurerm_subnet" "subnet-2" {
   name= var.subnet_name2
   resource_group_name  = var.rgname
   virtual_network_name = azurerm_virtual_network.vnet.name
   address_prefix       = var.subnet2_address_prefix 

  
}
#Subnet Creation
resource "azurerm_subnet" "subnet-3" {
  name= var.subnet_name3
  name= var.subnet_name1
   resource_group_name  = var.rgname
   virtual_network_name = azurerm_virtual_network.vnet.name
   address_prefix       = var.subnet3_address_prefix 

  
}

我收到以下错误:

address_space = var.vnet_address_space |---------------- | var.vnet_address_space 是“” 属性“address_space”的值不合适:需要字符串列表。*

我一开始就尝试为虚拟网络地址 space 提供一个 ip,因为它适用于单个子网。然后系统期待列表而不是字符串。知道如何设置虚拟网络的值吗?

您当前的var.vnet_address_space只是一个字符串:

vnet_address_space    = "<ip>"

但是 address_space 应该是一个 字符串列表 ,而不仅仅是一个字符串。

将其更改为字符串列表的最简单方法是:

vnet_address_space    = ["<ip>"]

或者,您可以按如下方式进行:

resource "azurerm_virtual_network" "vnet" {
 name                = var.vnet_name
 location            = var.loc
 resource_group_name = var.rgname
 address_space       = [var.vnet_address_space]
}