Skip to content

Commit 91df33f

Browse files
authored
Merge pull request #5 from data-platform-hq/fix/subnet-creation-optionality
fix: subnet creation optionality
2 parents 1014dfb + 2a3e4fb commit 91df33f

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Terraform module for creation Azure Network Subnet
99
| Name | Version |
1010
|------|---------|
1111
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
12-
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 3.23.0 |
12+
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 3.40.0 |
1313

1414
## Providers
1515

1616
| Name | Version |
1717
|------|---------|
18-
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >= 3.23.0 |
18+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | >= 3.40.0 |
1919

2020
## Modules
2121

@@ -36,10 +36,11 @@ No modules.
3636
| <a name="input_delegations"></a> [delegations](#input\_delegations) | (optional) subnet delegation | <pre>list(object({<br> name = string<br> actions = list(string)<br> }))</pre> | `[]` | no |
3737
| <a name="input_name"></a> [name](#input\_name) | The name of the subnet | `string` | n/a | yes |
3838
| <a name="input_network"></a> [network](#input\_network) | The name of the virtual network in which the subnet is created in | `string` | n/a | yes |
39-
| <a name="input_nsg_id"></a> [nsg\_id](#input\_nsg\_id) | The ID of the Network Security Group which should be associated with the Subnet | `string` | `""` | no |
39+
| <a name="input_nsg_id"></a> [nsg\_id](#input\_nsg\_id) | The ID of the Network Security Group which should be associated with the Subnet | `string` | null | no |
4040
| <a name="input_private_endpoint_network_policies_enabled"></a> [private\_endpoint\_network\_policies\_enabled](#input\_private\_endpoint\_network\_policies\_enabled) | Enable or Disable network policies for the private link endpoint on the subnet. Setting this to true will Disable the policy and setting this to false will Enable the policy: [true\|false] | `bool` | `true` | no |
4141
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | The name of the resource group in which to create the storage account | `string` | n/a | yes |
4242
| <a name="input_service_endpoints"></a> [service\_endpoints](#input\_service\_endpoints) | The list of Service endpoints to associate with the subnet: Microsoft.AzureActiveDirectory, Microsoft.AzureCosmosDB, Microsoft.ContainerRegistry, Microsoft.EventHub, Microsoft.KeyVault, Microsoft.ServiceBus, Microsoft.Sql, Microsoft.Storage, Microsoft.Web | `list(string)` | <pre>[<br> "Microsoft.Storage",<br> "Microsoft.KeyVault",<br> "Microsoft.Sql"<br>]</pre> | no |
43+
| <a name="input_export_subnet_id"></a> [export\_subnet\_id](#input\_export\_subnet\_id) | ID of already existing subnet. Provide this value to associate existing subnet with given Network Security Group | `string` | null | no |
4344

4445
## Outputs
4546

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
resource "azurerm_subnet" "this" {
2+
count = var.export_subnet_id == null ? 1 : 0
3+
24
name = var.name
35
resource_group_name = var.resource_group
46
virtual_network_name = var.network
@@ -19,8 +21,8 @@ resource "azurerm_subnet" "this" {
1921
}
2022

2123
resource "azurerm_subnet_network_security_group_association" "this" {
22-
count = var.nsg_id == "" ? 0 : 1
24+
count = var.nsg_id == null ? 0 : 1
2325

24-
subnet_id = azurerm_subnet.this.id
26+
subnet_id = var.export_subnet_id == null ? azurerm_subnet.this[0].id : var.export_subnet_id
2527
network_security_group_id = var.nsg_id
2628
}

outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
output "id" {
2-
value = azurerm_subnet.this.id
2+
value = var.export_subnet_id == null ? azurerm_subnet.this[0].id : null
33
description = "The ID of the subnet"
44
}
55

66
output "name" {
7-
value = azurerm_subnet.this.name
7+
value = var.export_subnet_id == null ? azurerm_subnet.this[0].name : null
88
description = "The name of the subnet"
99
}
1010

1111
output "address_prefixes" {
12-
value = azurerm_subnet.this.address_prefixes
12+
value = var.export_subnet_id == null ? azurerm_subnet.this[0].address_prefixes : null
1313
description = "The address prefixes to use for the subnet"
1414
}
1515

1616
output "nsg_association_id" {
17-
value = var.nsg_id == "" ? "" : azurerm_subnet_network_security_group_association.this[0].id
17+
value = azurerm_subnet_network_security_group_association.this[0].id
1818
description = "The ID of the Network Security Group Association"
1919
}
2020

2121
output "name_to_id_map" {
22-
value = { (azurerm_subnet.this.name) = azurerm_subnet.this.id }
22+
value = var.export_subnet_id == null ? { (azurerm_subnet.this[0].name) = azurerm_subnet.this[0].id } : null
2323
description = "Map of Subnet Name to Id"
2424
}

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,11 @@ variable "delegations" {
4747
variable "nsg_id" {
4848
type = string
4949
description = "The ID of the Network Security Group which should be associated with the Subnet"
50-
default = ""
50+
default = null
51+
}
52+
53+
variable "export_subnet_id" {
54+
type = string
55+
description = "ID of already existing subnet. Provide this value to associate existing subnet with given Network Security Group"
56+
default = null
5157
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
azurerm = {
66
source = "hashicorp/azurerm"
7-
version = ">= 3.23.0"
7+
version = ">= 3.40.0"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)