<p>Is something like the below what you are looking for?</p>
<pre><code>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
}
</code></pre>

CC BY-SA 4.0
2020-12-28T20:49:36.023Z
2020-12-28T20:49:36.023Z
Mikhail Advani
<p>I need to do the following in Terraform but can't work out how</p>
<p>Here is what I have</p>
<pre><code>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
}
</code></pre>
<p>What I can't work out is how you do the following:
For every app_config defined, for example:
test-web
test-api</p>
<p>A Key Vault secret are created by the environment defined when running:</p>
<p>Terraform plan or apply -var=&quot;environment=dev&quot;</p>
<p>I want it to create the 3 secrets for each app_config</p>

<terraform>
Terraform foreach multiple map variables
stravze
# Terraform foreach multiple map variables

(Asked by: username_0 on 2020-11-22)

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            = "test-web-${local.environment}"
      websockets      = false
      subnet          = "backend"
      audience_subnet = "frontend"
      url             = "https://test-web-${local.environment}.mydomain.com"
    }
    test-api  = {
      name            = "test-api-${local.environment}"
      websockets      = false
      subnet          = "backend"
      audience_subnet = "frontend"
      url             = "https://test-api-${local.environment}.mydomain.com"
    }
  }


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

resource "azurerm_key_vault_secret" "app_id" {
  for_each = var.apps_config

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

resource "azurerm_key_vault_secret" "client_id" {
  for_each = var.apps_config

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

resource "azurerm_key_vault_secret" "admin_pass" {
  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="environment=dev"
I want it to create the 3 secrets for each app_config

**Tags:** terraform


---

## 1 answers

### Answer by username_1 on 2020-12-28. Score: 2

Is something like the below what you are looking for?
locals {
  apps = keys(var.app_config)
}

resource "azurerm_key_vault_secret" "app_id" {
  for_each = toset(local.apps)

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


---

