Azure subnet delegation for selected subnets using for_each

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 = [“”]
mlops-aue-snt-stg = [“”]
mlops-aue-snt-kv = [“”]
AzureBastionSubnet = [“”]
mlops-aue-snt-shd = [“”]
mlops-aue-snt-db1 = [“”]
mlops-aue-snt-db2 = [“”]
mlops-aue-snt-aci = [“”]

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

name = each.key
resource_group_name =
virtual_network_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.