Example: I want to create 2 vnets using variables and create 2 subnets one vnet and 3 subnets in one vnet.
-
Thanks man @Charles Xu, it's working. – Yeshwanth Reddy Aug 20 '19 at 15:05
For your requirement, the example code here:
variable "vnet_address_space" {
default = "10.0.0.0/16"
}
variable "subnet_prefix" {
default = [
"10.0.1.0/24",
"10.0.2.0/24"
]
}
resource "azurerm_resource_group" "test" {
name = "testResourceGroup1"
location = "East US"
}
resource "azurerm_virtual_network" "test" {
name = "acceptanceTestVirtualNetwork1"
address_space = ["${var.vnet_address_space}"]
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
count = "${length(var.subnet_prefix)}"
name = "testsubnet-${count.index}"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "${element(var.subnet_prefix, count.index)}"
}
You can create another Vnet in the same format and change something in it as you wish.