Estou tentando adicionar várias strings de conexão nas configurações de um aplicativo de função do Azure por meio do Terraform, mas estou recebendo um erro
resource "azurerm_linux_function_app" "ali_test_fa" {
name = local.function_app_function_app_name
resource_group_name = data.azurerm_resource_group.sandbox_resource_group.name
location =
data.azurerm_resource_group.sandbox_resource_group.location
service_plan_id = azurerm_service_plan.ali_test_asp.id
storage_account_name = azurerm_storage_account.ali_test_fa_st.name
storage_account_access_key = azurerm_storage_account.ali_test_fa_st.primary_access_key
tags = local.tags
dynamic "connection_string" {
for_each = toset(local.connection_strings)
content {
name = each.value.name
type = each.value.type
value = each.value.value
}
}
Error: each.value cannot be used in this context
│
│ on resource_linux_function-app.tf line 26, in resource "azurerm_linux_function_app" "ali_test_fa":
│ 26: name = each.value.name
│
│ A reference to "each.value" has been used in a context in which it is unavailable, such as when the configuration no longer contains the value in its
│ "for_each" expression. Remove this reference to each.value in your configuration to work around this error.
╵
╷
│ Error: each.value cannot be used in this context
│
│ on resource_linux_function-app.tf line 27, in resource "azurerm_linux_function_app" "ali_test_fa":
│ 27: type = each.value.type
│
│ A reference to "each.value" has been used in a context in which it is unavailable, such as when the configuration no longer contains the value in its
│ "for_each" expression. Remove this reference to each.value in your configuration to work around this error.
╵
╷
│ Error: each.value cannot be used in this context
│
│ on resource_linux_function-app.tf line 28, in resource "azurerm_linux_function_app" "ali_test_fa":
│ 28: value = each.value.value
│
│ A reference to "each.value" has been used in a context in which it is unavailable, such as when the configuration no longer contains the value in its
│ "for_each" expression. Remove this reference to each.value in your configuration to work around this error.
Especifiquei as strings de conexão como a seguir:
local = {
connection_strings = [
{
value = "I am a sensitive sql database connection string"
type = "SQLServer"
name = "connection_string_sql_database"
}
]
}
Tentei o seguinte, mas não obtive sucesso:
for_each = local.connection_strings
or
for_each = { for inst in local.connection_strings : inst.name => inst }
Atualização 1 : Parece que o seguinte funcionou
dynamic "connection_string" {
for_each = local.connection_strings
content {
name = connection_string.value.name
type = connection_string.value.type
value = connection_string.value.value
}
}
O erro
each.value cannot be used in this context
significa que não há nadaeach.value
para fazer referência aqui com odynamic "connection_string"
bloco, pois você está definindo a entrada como uma lista de mapas.Modifique o
dynamic
bloco conforme mostrado abaixo se você estiver passando valores por definição nolocals
bloco.main.tf
:Saída :
Alternativamente , você pode usar
variable
block em vezlocals
de pegar a entrada como list(object) como mostrado abaixo.dynamic
bloquear emmain.tf
:Referência : Registro de modelo - azurerm_linux_function_app Github para problemas relevantes.