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

将Azure Log Analytics Workspace转为模块遇属性错误及输出配置咨询

Terraform部署多区域Log Analytics Workspace问题解决

1. 解决"Unsupported attribute"错误

错误核心原因:你引用的module.rg[each.key].resource_group_name属性不存在,因为你的RG模块(../../../modules/common/rg)没有定义对应的输出项。

解决步骤:

  • 打开modules/common/rg/outputs.tf(不存在则新建),添加资源组名称的输出(注意:资源名要和RG模块main.tf里的资源定义一致,比如RG模块里的资源是azurerm_resource_group.this,就把rg改成this):
output "resource_group_name" {
  value = azurerm_resource_group.rg.name
}

修改完成后,module.rg的每个实例会暴露resource_group_name属性,main.tf里的resource_group_name = module.rg[each.key].resource_group_name就能正常运行。

2. 在main.tf所在目录创建循环输出

因为用for_each创建了多个LAW实例,module.law是map类型对象(key对应var.locations里的键,比如ause、auea),需要用Terraform的for表达式遍历输出。在main.tf所在目录新建outputs.tf,按需添加以下内容:

输出所有LAW的ID

output "law_workspace_ids" {
  description = "所有Log Analytics Workspace的ID"
  type        = map(string)
  value       = { for key, law_instance in module.law : key => law_instance.workspace_id }
}

输出所有LAW的名称

output "law_workspace_names" {
  description = "所有Log Analytics Workspace的名称"
  type        = map(string)
  value       = { for key, law_instance in module.law : key => law_instance.workspace_name }
}

输出包含多属性的LAW详情

如果需要同时输出ID、名称、位置等信息,可定义map(object)类型的输出(需先在LAW模块的outputs.tf新增location输出):

# 先在modules/common/law/outputs.tf添加
output "location" {
  value = azurerm_log_analytics_workspace.workspaces.location
}

# 再在main.tf所在目录的outputs.tf添加
output "law_workspace_details" {
  description = "所有Log Analytics Workspace的详细信息"
  type = map(object({
    id          = string
    name        = string
    location    = string
    rg_name     = string
  }))
  value = {
    for key, law_instance in module.law : key => {
      id          = law_instance.workspace_id
      name        = law_instance.workspace_name
      location    = law_instance.location
      rg_name     = module.rg[key].resource_group_name
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 02:13:18