Hi!
I’m quite inexperienced with terraform, and I’m spinning my head around something I guess should be quite easy to overcome:
I have a small module to create azure vnet and x number of subnets. The call for the module with the input variables looks like this:
module "vnet" {
source = "../module_vnet"
location = var.location
environment = var.environment
resource_group_name = azurerm_resource_group.networktest.name
vnet_name = "terraform"
address_space = ["10.2.0.0/22"]
subnet_count = 2
delegations = ["Microsoft.ContainerInstance/containerGroups",]
}
the module itself looks like this (without all the network security groups and rules):
data "azurerm_resource_group" "main" {
name = var.resource_group_name
}
resource "azurerm_virtual_network" "vnet" {
name = "vnet-${var.vnet_name}-${var.environment}"
tags = data.azurerm_resource_group.main.tags
location = var.location
address_space = var.address_space
resource_group_name = var.resource_group_name
dns_servers = var.dns_servers
}
resource "azurerm_subnet" "subnet" {
count = var.subnet_count
name = "${var.vnet_name}-subnet-${count.index}"
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = var.resource_group_name
address_prefix = cidrsubnet(var.address_space[0], 2, count.index)
enforce_private_link_endpoint_network_policies = var.private_link
dynamic "delegation" {
for_each = var.delegations
content {
name = delegation.value
service_delegation {
name = delegation.value
actions = ["Microsoft.Network/virtualNetworks/subnets/action"]
}
}
}
}
This will off course create 2 subnets where both have enabled service delegations. However, what I want is somehow to define which subnet should have delegations enabled.
How should I proceed to do this? I really like the idea of just changing the subnet_count to add/subtract subnets without need to enter a lot of information, so I would really like to keep this logic.