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

Terraform报错‘Object Required’:变量与tfvars配置不匹配求助

问题原因

你遇到的错误核心是变量类型定义与实际赋值不匹配:
在变量定义中,SFTP被声明为map(object({...})),这意味着SFTP必须是一个键值对映射结构(每个键对应一个包含containers和users的对象);但在base.auto.tfvars里,你直接将SFTP赋值为单个对象,没有符合map类型要求的键值结构,导致Terraform解析时出错。

两种解决方案

方案一:修改变量定义(推荐,若仅需单组SFTP配置)

如果你的场景只需要一组SFTP配置,把SFTP的类型从map(object(...))改为object(...),让变量结构和赋值匹配:

variable "environment" {
  type = object({
    SFTP = object({  # 移除外层的map()包裹
      containers = list(object({
        name                  = string
        container_access_type = optional(string)
        metadata              = optional(map(string))
      }))
      users = list(object({
        name            = string
        home_directory  = optional(string)
        ssh_key_enabled = optional(bool, true)
        permissions_scopes = list(object({
          target_container = string
          permissions      = optional(list(string), ["All"])
        }))
        ssh_authorized_keys = optional(list(object({
          key         = string
          description = optional(string)
        })), [])
      }))
    })
  })
}

修改后无需调整base.auto.tfvars的内容,直接执行Terraform即可。

方案二:修改tfvars文件(若需多组SFTP配置)

如果你的场景需要支持多组SFTP配置(保留map类型),给SFTP添加键值结构,比如新增一个标识键(如primary):

environment = {
  SFTP = {
    primary = {  # 添加键,对应原有的SFTP配置对象
      containers = [
        {
          name                  = "sftp-container-1"
          container_access_type = "public"
          metadata              = {
            environment = "production"
          }
        }
      ]
      users = [
        {
          name            = "user1"
          home_directory  = "/home/user1"
          ssh_key_enabled = true
          permissions_scopes = [
            {
              target_container = "sftp-container-1"
              permissions      = ["Read", "Write"]
            },
            {
              target_container = "sftp-container-2"
              permissions      = ["All"]
            }
          ]
          ssh_authorized_keys = [
            {
              key         = "ssh-rsa AAAAB3Nza user1@example.com"
              description = "User 1's SSH key"
            }
          ]
        }
      ]
    }
  }
}

这样SFTP就符合map(object(...))的类型要求,每个键对应一组独立的SFTP配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 01:51:03