Terraform部署Azure Linux Function Apps时类型匹配错误求助
Terraform Azure函数应用配置错误修复
错误信息
"The given value is not suitable for var.function_apps declared at variables.tf:307,1-25: element 0: attribute "site_config": element "always_on": object required."
问题根源
variables.tf中site_config的类型定义错误:将其声明为map(object({...}))(对象的映射结构),但实际在terraform.tfvars里site_config是一个直接的单个对象,而非映射。类型不匹配导致Terraform无法正确解析always_on字段。同时main.tf存在逻辑错误:直接从each.value中查找always_on和linux_fx_version,但这两个字段实际嵌套在site_config对象下。
涉及文件内容
main.tf
resource "azurerm_linux_function_app" "function_apps" { for_each = { for idx, app in var.function_apps : idx => app } name = each.value["name"] location = var.azure_location resource_group_name = var.resource_group_name storage_account_name = var.storage_account_name storage_account_access_key = var.storage_account_access_key service_plan_id = azurerm_app_service_plan.app_plan.id site_config { always_on = lookup(each.value, "always_on", true) linux_fx_version = lookup(each.value, "linux_fx_version", "") } app_settings = lookup(each.value, "app_settings", {}) identity { type = lookup(each.value, "identity_type", "SystemAssigned") } tags = var.tags }
variables.tf
variable "function_apps" { description = "List of maps containing configurations for each Azure Container Group." type = list(object({ name = string version = string site_config = map(object({ always_on = bool linux_fx_version = string })) app_settings = optional(map(string)) identity_type = optional(string) })) default = [] }
terraform.tfvars
function_apps = [ { name = "s3-copier" version = " ~3" site_config = { always_on = true linux_fx_version = "DOCKER|mydockerregistry/myfunctionapp:latest" } app_settings = { FUNCTIONS_WORKER_RUNTIME = "dotnet" WEBSITE_RUN_FROM_PACKAGE = "1" } identity_type = "SystemAssigned" }, { name = "notification-processor" version = " ~3" site_config = { always_on = true linux_fx_version = "DOCKER|mydockerregistry/myfunctionapp:latest" } app_settings = { "FUNCTIONS_WORKER_RUNTIME" = "node" "WEBSITE_RUN_FROM_PACKAGE" = "1" } identity_type = "SystemAssigned" }, { name = "upload-processor" version = " ~3" site_config = { always_on = true linux_fx_version = "DOCKER|mydockerregistry/myfunctionapp:latest" } app_settings = { "FUNCTIONS_WORKER_RUNTIME" = "node" "WEBSITE_RUN_FROM_PACKAGE" = "1" } identity_type = "SystemAssigned" }, ]
修复方案
1. 修正variables.tf的类型定义
将site_config的类型从map(object({...}))改为object({...}),匹配tfvars中的实际结构:
variable "function_apps" { description = "List of configurations for each Azure Linux Function App." type = list(object({ name = string version = string site_config = object({ always_on = bool linux_fx_version = string }) app_settings = optional(map(string)) identity_type = optional(string) })) default = [] }
2. 修正main.tf的字段访问逻辑
将site_config块中的字段引用改为从each.value.site_config中获取:
site_config { always_on = each.value.site_config.always_on linux_fx_version = each.value.site_config.linux_fx_version }
如果需要为字段设置默认值(避免个别应用未配置时出错),可以用lookup:
site_config { always_on = lookup(each.value.site_config, "always_on", true) linux_fx_version = lookup(each.value.site_config, "linux_fx_version", "") }
验证修复
修改完成后,执行以下命令验证配置:
terraform init terraform validate
验证通过后即可正常执行terraform plan和terraform apply。
内容的提问来源于stack exchange,提问作者Susheel Bhatt
相关产品推荐
相关产品推荐

