Search code examples
terraform

Terraform Error: Object Required, I can't find the error in the variable spec


I have the following variable declaration:

variable "environment" {
  type = object({
    SFTP = map(object({
      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)
        })), [])
      }))
    }))
  })
}

and for it the base.auto.tfvars file:

environment = {
  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"
          }
        ]
      }
    ]
  }
}

and running this, Terraform says: The given value is not suitable for var.environment declared at infra-variables.tf:1,1-23: attribute "SFTP": element "containers": object required.

I'm sorry for this silly post, but I can't find it.

Thanks Wolfgang


Solution

  • The type definition clashes with the value because SFTP's value should be a map(object), but instead an object is provided as the value. It is unclear which you intended, and so if you intended object you update the definition like:

    type = object({
      SFTP = object({
        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)
          })), [])
        }))
      })
    })
    

    but if you intended map(object) then you update the value like:

    environment = {
      SFTP = {
        "some_descriptive_key" = {
          ...
        }
      }
    }