resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
resource_group_name = var.rg_name
location = var.location
address_space = var.vnet_address_space
dns_servers = var.dns_servers
tags = var.tags
}
resource "azurerm_subnet" "subnet" {
for_each = var.subnets
name = each.key
resource_group_name = var.rg_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = toset(each.value.address_prefixes)
service_endpoints = toset(each.value.service_endpoints)
service_endpoint_policy_ids = toset(each.value.service_endpoint_policy_ids)
private_endpoint_network_policies_enabled = each.value.private_endpoint_network_policies_enabled
private_link_service_network_policies_enabled = each.value.private_link_service_network_policies_enabled
dynamic "delegation" {
for_each = each.value.delegation != null ? each.value.delegation : []
content {
name = delegation.value.type
service_delegation {
name = delegation.value.type
actions = lookup(var.subnet_delegations_actions, delegation.value.type, delegation.value.action)
}
}
}
}
locals {
subnets = {
for subnet in azurerm_subnet.subnet :
subnet.name => subnet.id
}
}
resource "azurerm_subnet_network_security_group_association" "vnet" {
for_each = var.nsg_ids != null ? var.nsg_ids : {}
subnet_id = local.subnets[each.key]
network_security_group_id = each.value
}
locals {
route_table_associations = { for assoc in azurerm_subnet_route_table_association.this : assoc.id => { subnet_id = assoc.subnet_id, route_table_id = assoc.route_table_id } }
grouped_by_route_table = { for rt_id in distinct([for assoc in local.route_table_associations : local.route_table_associations[assoc].route_table_id]) :
rt_id => [for assoc in local.route_table_associations : local.route_table_associations[assoc].subnet_id if local.route_table_associations[assoc].route_table_id == rt_id]
}
}
resource "azurerm_route_table" "this" {
for_each = var.route_tables
name = each.key
location = var.location
resource_group_name = var.rg_name
disable_bgp_route_propagation = false
dynamic "route" {
for_each = each.value.routes
content {
name = route.key
address_prefix = route.value.address_prefix
next_hop_type = route.value.next_hop_type
next_hop_in_ip_address = lookup(route.value, "next_hop_in_ip_address", null)
}
}
}
resource "azurerm_subnet_route_table_association" "this" {
depends_on = [azurerm_subnet.subnet]
for_each = var.subnet_route_table_associations
subnet_id = local.subnets[each.key]
route_table_id = azurerm_route_table.this[each.value].id
}
No requirements.
Name | Version |
---|---|
azurerm | n/a |
No modules.
Name | Type |
---|---|
azurerm_route_table.this | resource |
azurerm_subnet.subnet | resource |
azurerm_subnet_network_security_group_association.vnet | resource |
azurerm_subnet_route_table_association.this | resource |
azurerm_virtual_network.vnet | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
dns_servers | The DNS servers to be used with vNet. | list(string) |
[] |
no |
location | The location for this resource to be put in | string |
n/a | yes |
nsg_ids | A map of subnet name to Network Security Group IDs | map(string) |
{} |
no |
rg_name | The name of the resource group, this module does not create a resource group, it is expecting the value of a resource group already exists | string |
n/a | yes |
route_tables | Map of Route Tables to be created, where the key is the name of the Route Table. | map(object({ |
{} |
no |
route_tables_ids | A map of subnet name to Route table ids | map(string) |
{} |
no |
subnet_delegations_actions | List of delegation actions when delegations of subnets is used, will be done for query | map(list(string)) |
{ |
no |
subnet_enforce_private_link_endpoint_network_policies | A map of subnet name to enable/disable private link endpoint network policies on the subnet. | map(bool) |
{} |
no |
subnet_enforce_private_link_service_network_policies | A map of subnet name to enable/disable private link service network policies on the subnet. | map(bool) |
{} |
no |
subnet_route_table_associations | Map where the key is the subnet name and the value is the name of the route table to associate with. | map(string) |
{} |
no |
subnet_service_endpoints | A map of subnet name to service endpoints to add to the subnet. | map(any) |
{} |
no |
subnets | Map of subnets with their properties | map(object({ |
{} |
no |
tags | The tags to associate with your network and subnets. | map(string) |
n/a | yes |
vnet_address_space | The address space that is used by the virtual network. | list(string) |
n/a | yes |
vnet_location | The location of the vnet to create. Defaults to the location of the resource group. | string |
n/a | yes |
vnet_name | Name of the vnet to create | string |
n/a | yes |
Name | Description |
---|---|
route_table_ids | Map of Route Table names to their IDs. |
subnet_ids_associated_with_route_tables | The IDs of the subnets associated with each route table |
subnets_ids | The ids of the subnets created |
subnets_names | The name of the subnets created |
vnet_address_space | The address space of the newly created vNet |
vnet_dns_servers | The dns servers of the vnet, if it is using Azure default, this module will return the Azure 'wire' IP as a list of string in the 1st element |
vnet_id | The id of the newly created vNet |
vnet_location | The location of the newly created vNet |
vnet_name | The Name of the newly created vNet |
vnet_rg_name | The resource group name which the VNet is in |