Imad
Imad

Reputation: 2741

How to describe an object type variable in Terraform?

I am trying to write a clear documentation/description of my terraform modules. According to Hashicorp's doc, the description attribute will permit that, but I cannot find a way to describe an object type variable in details.

Here's more or less I want to do :

variable "sa_to_impersonate_info" {
  type = object(
    {
      id                   = string
      email                = string
      belonging_org_id     = string
      belonging_project_id = string
      token_scopes         = list(string)
      token_lifetime       = string
    }
  )
  description = {
    id : "an identifier for the resource with format projects/{{project}}/serviceAccounts/{{email}}"
    email : "Email of the service account to impersonate"
    belonging_org_id : "Organization ID where the service account belongs"
    belonging_project_id : "Porject ID where the service account belongs"
    token_scopes : "List of scopes affected by this service account impersonation"
    token_lifetime : "Time the token will be active"
  }
}

For this format I get this error when I perform a terraform plan:

 Error: Unsuitable value type

Upvotes: 8

Views: 20619

Answers (2)

harshavmb
harshavmb

Reputation: 3872

You can have it in below format with EOT delimiter.

variable "sa_to_impersonate_info" {
  type = object(
    {
      id                   = string
      email                = string
      belonging_org_id     = string
      belonging_project_id = string
      token_scopes         = list(string)
      token_lifetime       = string
    }
  )
  description = <<EOT
    sa_to_impersonate_info = {
      id : "an identifier for the resource with format projects/{{project}}/serviceAccounts/{{email}}"
      email : "Email of the service account to impersonate"
      belonging_org_id : "Organization ID where the service account belongs"
      belonging_project_id : "Porject ID where the service account belongs"
      token_scopes : "List of scopes affected by this service account impersonation"
      token_lifetime : "Time the token will be active"
    }
  EOT
}

Upvotes: 9

Imad
Imad

Reputation: 2741

Here's a way to print all the fields as one string using terraform's heredoc... (Probably not the best solution)

variable "sa_to_impersonate_info" {
  type = object(
    {
      id                   = string
      email                = string
      belonging_org_id     = string
      belonging_project_id = string
      token_scopes         = list(string)
      token_lifetime       = string
    }
  )
description = <<-_EOT
  {
    id : "an identifier for the resource with format projects/{{project}}/serviceAccounts/{{email}}"
    email = Email of the service account to impersonate
    belonging_org_id = Organization ID where the service account belongs
    belonging_project_id = Porject ID where the service account belongs
    token_scopes = List of scopes affected by this service account impersonation
    token_lifetime = Time the token will be active
  }
  _EOT
}

Use <<-_EOT to take into account the indentation, <<_EOT otherwise

Upvotes: 2

Related Questions