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

Terraform locals合并tfvars值失效问题求助

问题根源与解决方法

你的核心问题是merge函数的参数顺序错误,导致tfvars传入的字段(包括null值)覆盖了你自定义的默认逻辑计算结果。

Terraform的merge函数规则是:参数序列中靠后的映射/对象,同名键的优先级更高。你当前的写法是merge(自定义逻辑对象, law),这意味着tfvars里的law对象会覆盖前面自定义逻辑计算出的所有同名字段——哪怕law里的字段值是null。

具体问题分析

  1. location字段失效:当tfvars里的location设为null时,自定义逻辑中law.location != null ? law.location : ...本应生成有效的resource group location,但因为后面merge了law,最终location会被覆盖为null,触发资源创建时的必填参数错误。
  2. tags合并失效:你前面通过merge(data.azurerm_resource_group.resource_groups[law.name].tags, law.tags)生成了合并后的标签,但后续mergelaw时,如果law里存在tags字段(哪怕是空对象或null),会直接覆盖前面的合并结果。

修正后的代码

调整merge的参数顺序,把law放在前面,自定义逻辑对象放在后面,让自定义逻辑覆盖law中的null值或缺失字段:

##Data Source for Resource Groups
data "azurerm_resource_group" "resource_groups" {
  for_each = { for law in var.log_analytics_workspace : law.name => law }
  name     = each.value.resource_group_name
}

###LOCALS###
locals {
  laws = { for law in var.log_analytics_workspace : law.name => merge(
    law,
    {
      location                           = law.location != null ? law.location : data.azurerm_resource_group.resource_groups[law.name].location
      sku                                = coalesce(law.sku, "PerGB2018")
      retention_in_days                  = coalesce(law.retention_in_days, 30)
      daily_quota_gb                     = coalesce(law.daily_quota_gb, 1)
      internet_ingestion_enabled         = false
      internet_query_enabled             = false
      cmk_for_query_forced               = false
      reservation_capacity_in_gb_per_day = law.sku != "CapacityReservation" ? null : law.reservation_capacity_in_gb_per_day
      tags                               = merge(data.azurerm_resource_group.resource_groups[law.name].tags, law.tags)
    }
  ) }
}

#Log Analytics Resource
resource "azurerm_log_analytics_workspace" "log_analytics_workspace" {
  for_each                           = local.laws
  name                               = each.value.name
  resource_group_name                = each.value.resource_group_name
  location                           = each.value.location
  sku                                = each.value.sku
  retention_in_days                  = each.value.retention_in_days
  daily_quota_gb                     = each.value.daily_quota_gb
  internet_ingestion_enabled         = each.value.internet_ingestion_enabled
  internet_query_enabled             = each.value.internet_query_enabled
  cmk_for_query_forced               = each.value.cmk_for_query_forced
  reservation_capacity_in_gb_per_day = each.value.reservation_capacity_in_gb_per_day
  tags                               = each.value.tags
}

效果验证

  • 当tfvars里的location设为null时,自定义逻辑会生成resource group的location,且不会被law中的null覆盖。
  • tags会正确合并resource组的标签和tfvars传入的标签,且不会被law中的tags字段覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:05:26