Terraform foreach multiple map variables I need to do the following in Terraform but can't work out how
Here is what I have
local {
app_config = {
    test-web = {
      name            = &quot;test-web-${local.environment}&quot;
      websockets      = false
      subnet          = &quot;backend&quot;
      audience_subnet = &quot;frontend&quot;
      url             = &quot;https://test-web-${local.environment}.mydomain.com&quot;
    }
    test-api  = {
      name            = &quot;test-api-${local.environment}&quot;
      websockets      = false
      subnet          = &quot;backend&quot;
      audience_subnet = &quot;frontend&quot;
      url             = &quot;https://test-api-${local.environment}.mydomain.com&quot;
    }
  }


keyvault_secrets = {
    aat = {
        test-api = {
            application_id = &quot;1111&quot;
            client_id = &quot;2222&quot;
            administrator_pass = &quot;3333&quot;
        }
        test-web = {
            application_id = &quot;4444&quot;
            client_id = &quot;5555&quot;
            administrator_pass = &quot;6666&quot;
        }
    }
    Demo = {
        test-api = {
            application_id = &quot;1212&quot;
            client_id = &quot;2323&quot;
            administrator_pass = &quot;3434&quot;
        }
        test-web = {
            application_id = &quot;4545&quot;
            client_id = &quot;5656&quot;
            administrator_pass = &quot;6767&quot;
        }
    }
    Dev = {
        test-api = {
            application_id = &quot;9999&quot;
            client_id = &quot;8888&quot;
            administrator_pass = &quot;7777&quot;
        }
        test-web = {
            application_id = &quot;9898&quot;
            client_id = &quot;8787&quot;
            administrator_pass = &quot;7676&quot;
        }
    }
}

resource &quot;azurerm_key_vault_secret&quot; &quot;app_id&quot; {
  for_each = var.apps_config

  name         = var.apps_config
  value        = each.value.application_id
  key_vault_id = data.azurerm_key_vault.mykv.id
}

resource &quot;azurerm_key_vault_secret&quot; &quot;client_id&quot; {
  for_each = var.apps_config

  name         = var.apps_config
  value        = each.value.client_id
  key_vault_id = data.azurerm_key_vault.mykv.id
}

resource &quot;azurerm_key_vault_secret&quot; &quot;admin_pass&quot; {
  for_each = var.apps_config

  name         = var.apps_config
  value        = each.value.administrator_pass
  key_vault_id = data.azurerm_key_vault.mykv.id
}

What I can't work out is how you do the following:
For every app_config defined, for example:
test-web
test-api
A Key Vault secret are created by the environment defined when running:
Terraform plan or apply -var=&quot;environment=dev&quot;
I want it to create the 3 secrets for each app_config
Is something like the below what you are looking for?
locals {
  apps = keys(var.app_config)
}

resource &quot;azurerm_key_vault_secret&quot; &quot;app_id&quot; {
  for_each = toset(local.apps)

  name         = each.key
  value        = var.keyvault_secrets[var.environment][each.key][&quot;application_id&quot;]
  key_vault_id = data.azurerm_key_vault.mykv.id
}