Azure subnet delegation for selected subnets using for_each

Hi,
I want some help on creating delegation on selected azure subnets. My code is as per below details.

Variables defined in my variable file
variable “subnets” {
type = map(any)
}

My tfvar file contains below values
subnets = {
mlops-aue-snt-aks = [“10.255.232.0/24”]
mlops-aue-snt-stg = [“10.255.233.0/26”]
mlops-aue-snt-kv = [“10.255.233.128/27”]
AzureBastionSubnet = [“10.255.233.160/27”]
mlops-aue-snt-shd = [“10.255.234.0/25”]
mlops-aue-snt-db1 = [“10.255.235.0/26”]
mlops-aue-snt-db2 = [“10.255.235.64/26”]
mlops-aue-snt-aci = [“10.255.235.128/26”]
}

This is my code for subnet
resource “azurerm_subnet” “azr_subnet” {
for_each = var.subnets

name = each.key
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = each.value
enforce_private_link_endpoint_network_policies = true
}

All subnets are created with this and it is all fine but now I have a requirement to add service_delegation for mlops-aue-snt-db1 and mlops-aue-snt-db2 and different for other subnets. I am not sure how to achieve this with my existing code. I can’t separate out subnets from the code as it will force to delete existing ones and create new which is not recommended.

Can you please help me achieve this with my existing code as how do I do that?

Hi Everyone,

I still have no luck getting around the solution. I did read some posts about using dynamic block to make changes but not sure how to implement it for selective subnets as per my requirement.