You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Terraform配置Azure Function App多连接字符串报错及解决

配置Azure Linux Function App多连接字符串的Terraform报错解决

问题场景

使用Terraform为Azure Linux Function App配置多个连接字符串时触发报错。

原配置代码

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.

连接字符串本地变量定义

local = {
  connection_strings = [
    {
      value = "I am a sensitive sql database connection string"
      type  = "SQLServer"
      name  = "connection_string_sql_database"
    }
  ]
}

未生效的修改尝试

  • 使用for_each = local.connection_strings
  • 使用for_each = { for inst in local.connection_strings : inst.name => inst }

可行的配置方案

调整dynamic块内的引用方式,使用动态块的名称替代each.value:

dynamic "connection_string" {
  for_each = local.connection_strings
  content {
    name  = connection_string.value.name
    type  = connection_string.value.type
    value = connection_string.value.value
  }
}

内容的提问来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:57:47