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

使用Terraform遍历Map变量批量创建Databricks Catalog异常排查

Terraform批量创建Databricks Catalog时无法检测状态差异问题

尝试用Terraform的for_each遍历Map类型变量批量创建多个Databricks Catalog,但Terraform无法检测到与现有状态的差异;单独创建Catalog的配置可正常运行,推测问题出在HCL循环语法上。

相关配置代码

variables.tf(变量定义文件)

variable "catalog" {
  type = map(object({
    catalog_grants         = optional(map(list(string)))
    catalog_owner          = optional(string)         # Username/groupname/sp application_id of the catalog owner.
    catalog_storage_root   = optional(string)         # Location in cloud storage where data for managed tables will be stored
    catalog_isolation_mode = optional(string, "OPEN") # Whether the catalog is accessible from all workspaces or a specific set of workspaces. Can be ISOLATED or OPEN.
    catalog_comment        = optional(string)         # User-supplied free-form text
    catalog_properties     = optional(map(string))    # Extensible Catalog Tags.
    schema_name            = optional(list(string))   # List of Schema names relative to parent catalog.
    schema_grants          = optional(map(list(string)))
    schema_owner           = optional(string) # Username/groupname/sp application_id of the schema owner.
    schema_comment         = optional(string)
    schema_properties      = optional(map(string))
  }))
  description = "Map of catalog name and its parameters"
  default = {
    catalog = {
      example_catalog1 = {
        catalog_grants = {
          "example@username.com" = ["USE_CATALOG", "USE_SCHEMA", "CREATE_SCHEMA", "CREATE_TABLE", "SELECT", "MODIFY"]
        }
        schema_name = ["raw", "refined", "data_product"]
      }
      example_catalog2 = {
        catalog_grants = {
          "example@username.com" = ["USE_CATALOG", "USE_SCHEMA", "CREATE_SCHEMA", "CREATE_TABLE", "SELECT", "MODIFY"]
        }
        schema_name = ["raw", "refined", "data_product"]
      }
    }
  }
}

main.tf(循环创建资源文件)

resource "databricks_catalog" "this" {
  for_each = var.catalog

  name           = each.key
  owner          = each.value.catalog_owner
  storage_root   = each.value.catalog_storage_root
  isolation_mode = each.value.catalog_isolation_mode
  comment        = lookup(each.value, "catalog_comment", "default comment")
  properties     = lookup(each.value, "catalog_properties", {})
  force_destroy  = true
}

可正常运行的单独创建配置

resource "databricks_catalog" "sandbox" {
  name    = "example_catalog3"
}

问题原因及解决方案

问题根源

变量var.catalog的默认值多嵌套了一层catalog键,导致for_each实际遍历的是包含单个catalog键的Map,只会尝试创建一个名为catalog的Catalog,而非预期的example_catalog1和example_catalog2,因此Terraform无法识别到需要创建的目标资源,也就检测不到状态差异。

修复方案

方案一:修正变量结构(推荐)

修改variables.tf中的默认值,去掉外层多余的catalog键,让example_catalog1和example_catalog2直接作为Map的顶层键值对,匹配变量定义的map(object)类型结构:

variable "catalog" {
  # 其他定义保持不变
  default = {
    example_catalog1 = {
      catalog_grants = {
        "example@username.com" = ["USE_CATALOG", "USE_SCHEMA", "CREATE_SCHEMA", "CREATE_TABLE", "SELECT", "MODIFY"]
      }
      schema_name = ["raw", "refined", "data_product"]
    }
    example_catalog2 = {
      catalog_grants = {
        "example@username.com" = ["USE_CATALOG", "USE_SCHEMA", "CREATE_SCHEMA", "CREATE_TABLE", "SELECT", "MODIFY"]
      }
      schema_name = ["raw", "refined", "data_product"]
    }
  }
}

方案二:调整循环遍历对象

如果无法修改变量默认值结构,可直接在main.tf中指定遍历嵌套的Map:

resource "databricks_catalog" "this" {
  for_each = var.catalog.catalog
  # 其余配置保持不变
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 02:52:15