简体   繁体   English

使用 Terrafrom 的同一 VNet 中的多个子网

[英]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.

相关问题 Azure在具有多个子网的现有VNET上创建VM - Azure creating VM on existing VNET with multiple subnets vnet 中的多个子网需要通过专用终结点/服务终结点访问同一存储帐户 - Multiple subnets in vnet needing access to same storage account via Private Endpoint/Service Endpoint 使用AzureRM在同一vnet中部署不同子网的嵌套模板 - Nesting templates that deploy different subnets in the same vnet with AzureRM 如何在预先存在的 Vnet Powershell 中创建多个子网 - How to create multiple Subnets in pre-existing Vnet Powershell 如何使用二头肌在 azure eventHub 的网络部分中选择和添加 VNet 的多个子网 - How to select and add multiple subnets of a VNet in networking section of an azure eventHub using bicep 通过ARM模板将子网添加到现有Vnet - Addition of subnets to existing Vnet through ARM templates ARM 模板,用于与现有 VNET 子网集成的 Appservice - ARM template for Appservice integrating with existing VNET Subnets Terrafrom - 部署到多个 Azure 订阅 - Terrafrom - Deploy to multiple azure subscriptions 使用Azure VNet连接具有相同IP范围的多个本地网络 - Connecting multiple local network with same IP range with Azure VNet Azure su.net 中的 Output,具有指向附加 VNet、路由表和 NSG 的链接 - Output of Azure subnets with links to attached VNet, route table and NSG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM