[英]Multiple subnets in same VNet with Terrafrom
I'm completely new to terraform and I'm trying to learn and write a TF code to automate Azure VM deployment.我对 terraform 完全陌生,我正在尝试学习和编写 TF 代码来自动化 Azure VM 部署。 I'm trying to cover each parts as modules (except rg) rather than keeping it in a single main.tf file.我试图将每个部分作为模块(rg 除外)覆盖,而不是将其保存在单个 main.tf 文件中。 My intention is to create 1 vnet (TESTVNET) and create multiple subnets in same Vnet, where I can define the subnet name and address in my tfvars file.我的意图是创建 1 个 vnet (TESTVNET) 并在同一个 Vnet 中创建多个子网,我可以在我的 tfvars 文件中定义子网名称和地址。
I'm able to reach till creation on VNet, but cant loop through the defined subnets我能够到达直到在 VNet 上创建,但无法遍历定义的子网
Please go through my code.请通过我的代码。 File Main.tf<\/code>
文件
Main.tf<\/code>
resource "azurerm_resource_group" "resource_group" {
name = var.RGname
location = var.RGlocation
}
module "VNET" {
source = "./Modules/NetworkConfig"
name = var.VNETname
address_space = var.address_space
location = var.RGlocation
resource_group_name = azurerm_resource_group.resource_group.name
}
module "SUBNETS" {
source = "./Modules/SubnetConfig"
Subnetlist = var.Subnetlist
virtual_network_name = module.VNET.vnet_name
resource_group_name = azurerm_resource_group.resource_group.name
depends_on = [azurerm_resource_group.resource_group, module.VNET.vnet]
}
Variables.tf (of main)
variable "RGlocation" {
}
variable "RGname" {
}
variable "VNETname" {
}
variable "address_space" {
}
variable "Subnetlist" {
type = map(object({
name = list(string)
address = list(string)
}))
}
The way you iterate over Subnetlist<\/code> is incorrect - you only get the value of "list" key, ending up with a bundle of subnets instead of individual items.
迭代
Subnetlist<\/code>的方式是不正确的 - 您只能获得“list”键的值,最终得到的是一组子网而不是单个项目。
Make it a map of individual subnet objects instead:将其改为单个子网对象的映射:
variable "Subnetlist" {
type = map(object({
name = string
address = string
}))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.