- if I don’t want to provide route table for all the subnets in the virtual
network then what should I used here because if I run without route
table then I am getting error. route table id is required in subnet2.
│ Missing required argument
│
│ with module.virtual_network["test-vnet"].azurerm_subnet_route_table_association.route-link-subnet["subnet2"],
│ on modulesvirtualNetworkmain.tf line 30, in resource "azurerm_subnet_route_table_association" "route-link-subnet":
│ 30: route_table_id = each.value.route_table
│
│ The argument "route_table_id" is required, but no definition was found.
i am creating here multiple virtual network with multiple subnets on each vnet.
variable "networks" {
type = map(object({
resource_group_name = string
location = string
addressSpace = list(string)
dnsServers = optional(list(string))
subnets = map(object({
addressPrefix = string
privateEndpointNetworkPolicies = optional(bool)
privateLinkServiceNetworkPolicies = optional(bool)
service_endpoints = optional(list(string))
route_table = optional(string)
}))
}))
}
networks = {
test-vnet = {
resource_group_name = "testrg"
location = "eastus"
addressSpace = ["10.229.192.0/20"]
dnsServers = ["10.231.18.4"]
subnets = {
subnet1 = {
addressPrefix = "10.229.199.0/24"
networkSecurityGroups = {
subnet1-nsg = {
security_rules = []
}
}
route_table = "/subscriptions//resourceGroups/test-rg/providers/Microsoft.Network/routeTables/test-rt"
}
subnet2 = {
addressPrefix = "10.229.198.0/24"
privateEndpointNetworkPolicies = true
privateLinkServiceNetworkPolicies = true
service_endpoints = [
"Microsoft.Storage"
]
networkSecurityGroups = {
subnet2-nsg = {
security_rules = []
}
}
}
}
}
resource "azurerm_subnet_route_table_association" "route-link-subnet" {
for_each = var.subnets
subnet_id = module.subnets[each.key].subnet_id
route_table_id = each.value.route_table
depends_on = [
module.subnets,
azurerm_virtual_network.Virtual_Network
]
}
2
Answers
You have to provide
route_table
forsubnet2
in yourvar.subnets
.Your input variable’s type constraint disagrees with the requirements of
azurerm_subnet_route_table_association
: theroute_table_id
argument is required for that resource, so it isn’t valid to assignnull
to it.If you wish to only declare route table associations for subnets that have a non-null
route_table_id
then you can use afor
expression with anif
clause to filter thevar.subnets
value to include only the elements that have route table IDs:With the example above the
for_each
map will only include the subnets that haveroute_table
set, and so there will be no resource instance declared at all for the ones that do not have route tables.Note that this strategy will only be effective if the
route_table
attribute is always known during the plan phase of Terraform, because otherwise Terraform won’t be able to decide during planning how many instances of this resource are declared. What you showed in your question would work, where the route table ID is hard-coded into your object, but it would not work if the route table ID were derived from anotherresource
block whose remote object hasn’t been created yet.