From 66d3d539dbdf74a828b2131b3f3ce10759242fcd Mon Sep 17 00:00:00 2001 From: zjhe Date: Tue, 11 Oct 2022 16:51:11 +0800 Subject: [PATCH 1/2] Remove datasource `azurerm_resource_group.vnet`. Make `var.vnet_location` required. This change is a breaking change, and it will fix #62. --- README.md | 15 +++++++-------- examples/new_route/main.tf | 2 +- examples/new_security_rule/main.tf | 1 + examples/private_link_endpoint/main.tf | 1 + examples/private_link_service/main.tf | 1 + main.tf | 13 ++++--------- variables.tf | 12 ++++++------ 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 15941a6..f72d7a9 100644 --- a/README.md +++ b/README.md @@ -129,13 +129,12 @@ No modules. ## Resources -| Name | Type | -|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------| -| [azurerm_subnet.subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) | resource | -| [azurerm_subnet_network_security_group_association.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association) | resource | -| [azurerm_subnet_route_table_association.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_route_table_association) | resource | -| [azurerm_virtual_network.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) | resource | -| [azurerm_resource_group.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source | +| Name | Type | +|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------| +| [azurerm_subnet.subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) | resource | +| [azurerm_subnet_network_security_group_association.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association) | resource | +| [azurerm_subnet_route_table_association.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_route_table_association) | resource | +| [azurerm_virtual_network.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) | resource | ## Inputs @@ -154,7 +153,7 @@ No modules. | [subnet\_prefixes](#input\_subnet\_prefixes) | The address prefix to use for the subnet. | `list(string)` |
[
"10.0.1.0/24"
]
| no | | [subnet\_service\_endpoints](#input\_subnet\_service\_endpoints) | A map of subnet name to service endpoints to add to the subnet. | `map(any)` | `{}` | no | | [tags](#input\_tags) | The tags to associate with your network and subnets. | `map(string)` |
{
"ENV": "test"
}
| no | -| [vnet\_location](#input\_vnet\_location) | The location of the vnet to create. Defaults to the location of the resource group. | `string` | `null` | no | +| [vnet\_location](#input\_vnet\_location) | The location of the vnet to create. | `string` | n/a | yes | | [vnet\_name](#input\_vnet\_name) | Name of the vnet to create | `string` | `"acctvnet"` | no | ## Outputs diff --git a/examples/new_route/main.tf b/examples/new_route/main.tf index 7e9baf6..04f9c3a 100644 --- a/examples/new_route/main.tf +++ b/examples/new_route/main.tf @@ -10,10 +10,10 @@ resource "azurerm_resource_group" "example" { module "vnet" { source = "../../" resource_group_name = azurerm_resource_group.example.name + vnet_location = var.location address_space = ["10.0.0.0/16"] subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] subnet_names = ["subnet1", "subnet2", "subnet3"] - route_tables_ids = { subnet1 = azurerm_route_table.example.id subnet2 = azurerm_route_table.example.id diff --git a/examples/new_security_rule/main.tf b/examples/new_security_rule/main.tf index d688564..1d1ef2d 100644 --- a/examples/new_security_rule/main.tf +++ b/examples/new_security_rule/main.tf @@ -11,6 +11,7 @@ module "vnet" { source = "../../" resource_group_name = azurerm_resource_group.example.name + vnet_location = var.location address_space = ["10.0.0.0/16"] subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] subnet_names = ["subnet1", "subnet2", "subnet3"] diff --git a/examples/private_link_endpoint/main.tf b/examples/private_link_endpoint/main.tf index 9bc7688..db5fe29 100644 --- a/examples/private_link_endpoint/main.tf +++ b/examples/private_link_endpoint/main.tf @@ -10,6 +10,7 @@ resource "azurerm_resource_group" "example" { module "vnet" { source = "../../" resource_group_name = azurerm_resource_group.example.name + vnet_location = var.location address_space = ["10.0.0.0/16"] subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] subnet_names = ["subnet1", "subnet2", "subnet3"] diff --git a/examples/private_link_service/main.tf b/examples/private_link_service/main.tf index 9bc7688..db5fe29 100644 --- a/examples/private_link_service/main.tf +++ b/examples/private_link_service/main.tf @@ -10,6 +10,7 @@ resource "azurerm_resource_group" "example" { module "vnet" { source = "../../" resource_group_name = azurerm_resource_group.example.name + vnet_location = var.location address_space = ["10.0.0.0/16"] subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] subnet_names = ["subnet1", "subnet2", "subnet3"] diff --git a/main.tf b/main.tf index cd84b42..424df8c 100644 --- a/main.tf +++ b/main.tf @@ -1,13 +1,8 @@ -#Azure Generic vNet Module -data "azurerm_resource_group" "vnet" { - name = var.resource_group_name -} - resource "azurerm_virtual_network" "vnet" { address_space = var.address_space - location = var.vnet_location != null ? var.vnet_location : data.azurerm_resource_group.vnet.location + location = var.vnet_location name = var.vnet_name - resource_group_name = data.azurerm_resource_group.vnet.name + resource_group_name = var.resource_group_name dns_servers = var.dns_servers tags = var.tags @@ -26,7 +21,7 @@ resource "azurerm_subnet" "subnet" { address_prefixes = [var.subnet_prefixes[count.index]] name = var.subnet_names[count.index] - resource_group_name = data.azurerm_resource_group.vnet.name + resource_group_name = var.resource_group_name virtual_network_name = azurerm_virtual_network.vnet.name enforce_private_link_endpoint_network_policies = lookup(var.subnet_enforce_private_link_endpoint_network_policies, var.subnet_names[count.index], false) enforce_private_link_service_network_policies = lookup(var.subnet_enforce_private_link_service_network_policies, var.subnet_names[count.index], false) @@ -65,4 +60,4 @@ resource "azurerm_subnet_route_table_association" "vnet" { route_table_id = each.value subnet_id = local.azurerm_subnets[each.key] -} +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index fefde6e..357d34d 100644 --- a/variables.tf +++ b/variables.tf @@ -3,6 +3,12 @@ variable "resource_group_name" { type = string } +variable "vnet_location" { + description = "The location of the vnet to create." + type = string + nullable = false +} + variable "address_space" { type = list(string) description = "The address space that is used by the virtual network." @@ -84,12 +90,6 @@ variable "tags" { } } -variable "vnet_location" { - description = "The location of the vnet to create. Defaults to the location of the resource group." - type = string - default = null -} - variable "vnet_name" { description = "Name of the vnet to create" type = string From f8122c2c24ba69b15642bf95c98bf87f976cffd5 Mon Sep 17 00:00:00 2001 From: zjhe Date: Tue, 11 Oct 2022 21:46:09 +0800 Subject: [PATCH 2/2] set `var.resource_group_name`'s `nullable` to `false` --- variables.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/variables.tf b/variables.tf index 357d34d..649b71b 100644 --- a/variables.tf +++ b/variables.tf @@ -1,6 +1,7 @@ variable "resource_group_name" { description = "Name of the resource group to be imported." type = string + nullable = false } variable "vnet_location" {