Azure services used:
- Keyvault
- App Configuration
I was trying to store the secrets from keyvault that were created, on to app configuration. I was getting the below error and not sure on how to proceed. Kindly help me with your guidance.
│ Error: Invalid count argument
│
│ on ../../modules/appconfiguration/main.tf line 75, in data "azurerm_key_vault_secret" "kv_secret":
│ 75: count = length(data.azurerm_key_vault_secrets.kv_all_secrets.names)
│
│ The "count" value depends on resource attributes that cannot be determined
│ until apply, so Terraform cannot predict how many instances will be
│ created. To work around this, use the -target argument to first apply only
│ the resources that the count depends on.
All of the secret insertion to keyvault and extraction from keyvault is running on the same code. The app configuration needs to be referenced to the keyvault secrets.
Please find the code below for reference:
# Step 1: Fetching all the secrets from Keyvault
data "azurerm_key_vault_secrets" "kv_all_secrets" {
key_vault_id = data.azurerm_key_vault.kv.id
}
# Step 2: Looping in each secrets based on it name
data "azurerm_key_vault_secret" "kv_secret" {
count = length(data.azurerm_key_vault_secrets.kv_all_secrets.names)
name = data.azurerm_key_vault_secrets.kv_all_secrets.names[count.index]
key_vault_id = data.azurerm_key_vault.kv.id
}
# Step 3: Looping on each contents from previous data block to get the name and versionless Id of the secret stored
resource "azurerm_app_configuration_key" "app_config_key" {
#for_each = data.azurerm_key_vault_secret.kv_secret
count = length(data.azurerm_key_vault_secret.kv_secret)
configuration_store_id = data.azurerm_app_configuration.app_config.id
key = data.azurerm_key_vault_secret.kv_secret[count.index].name
type = "vault"
vault_key_reference = data.azurerm_key_vault_secret.kv_secret[count.index].versionless_id
}