Skip to main content
0 reactions

OpenTofu Variable Types Explained: string, number, bool, list, map and object

Card titled Variable Types Explained, showing eight chips with the OpenTofu types string, number, bool, list, set, map, object and any, each with an example value.
OpenTofu Variable Types Explained.

Every OpenTofu® input variable can have a type, which decides what values it accepts. Pick the right type and OpenTofu rejects a wrong value before it changes anything.

This guide declares one variable of each common type, shows how OpenTofu prints them, and what happens when a value has the wrong type.

Syntax

SyntaxHCL
variable "<NAME>" {
  type = <TYPE>
}

<TYPE> is one of these:

  • string: text, such as "Bookshop".
  • number: a whole or decimal number, such as 14.99.
  • bool: true or false.
  • list(<TYPE>): an ordered sequence of values of one type.
  • set(<TYPE>): values of one type with no order and no duplicates.
  • map(<TYPE>): named values of one type.
  • object({<NAME> = <TYPE>, …}): named attributes, each with its own type.
  • tuple([<TYPE>, …]): a fixed sequence with a type for each position.
  • any: any type. A variable without type accepts anything too.

The OpenTofu docs list these under Type Constraints (opens in a new tab).

Prerequisites

Step 1: Declare One Variable of Each Type

In an empty folder, create variables.tf. Each default matches its type:

variables.tfHCL
variable "shop_name" {
  type    = string
  default = "Bookshop"
}

variable "book_price" {
  type    = number
  default = 14.99
}

variable "open_on_sunday" {
  type    = bool
  default = true
}

variable "genres" {
  type    = list(string)
  default = ["Fiction", "Travel", "Poetry"]
}

variable "labels" {
  type    = set(string)
  default = ["sale", "new", "sale"]
}

variable "prices" {
  type = map(number)
  default = {
    "Salt and Starlight"     = 9.99
    "The Paper Lantern Café" = 14.99
  }
}

variable "featured_book" {
  type = object({
    title  = string
    author = string
    price  = number
  })
  default = {
    title  = "A Map of Quiet Places"
    author = "Tomas Vell"
    price  = 22.50
  }
}

Create outputs.tf, so OpenTofu prints every value:

outputs.tfHCL
output "shop_name" {
  value = var.shop_name
}

output "book_price" {
  value = var.book_price
}

output "open_on_sunday" {
  value = var.open_on_sunday
}

output "genres" {
  value = var.genres
}

output "labels" {
  value = var.labels
}

output "prices" {
  value = var.prices
}

output "featured_book" {
  value = var.featured_book
}

Step 2: See the Values

Apply and type yes. There are no resources, so OpenTofu only saves the outputs:

TerminalBash
tofu apply
OutputPlain text
Changes to Outputs:
  + book_price     = 14.99
  + featured_book  = {
      + author = "Tomas Vell"
      + price  = 22.5
      + title  = "A Map of Quiet Places"
    }
  + genres         = [
      + "Fiction",
      + "Travel",
      + "Poetry",
    ]
  + labels         = [
      + "new",
      + "sale",
    ]
  + open_on_sunday = true
  + prices         = {
      + "Salt and Starlight"      = 9.99
      + "The Paper Lantern Café"  = 14.99
    }
  + shop_name      = "Bookshop"

You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.

Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

book_price = 14.99
featured_book = {
  "author" = "Tomas Vell"
  "price" = 22.5
  "title" = "A Map of Quiet Places"
}
genres = tolist([
  "Fiction",
  "Travel",
  "Poetry",
])
labels = toset([
  "new",
  "sale",
])
open_on_sunday = true
prices = tomap({
  "Salt and Starlight" = 9.99
  "The Paper Lantern Café" = 14.99
})
shop_name = "Bookshop"
  • The list keeps its order. tolist(…) only shows that the value is a list.
  • The set dropped the second "sale" and sorted the rest, because a set has no duplicates and no order.
  • The map and the object both print names and values. In an object, each name can have its own type.

Step 3: Give a Value of the Wrong Type

Create terraform.tfvars with text where a number belongs:

terraform.tfvarsHCL
book_price = "free"
TerminalBash
tofu plan
OutputPlain text
│ Error: Invalid value for input variable
│ 
│   on variables.tf line 6:
│    6: variable "book_price" {
│ 
│ The given value is not suitable for var.book_price declared at variables.tf:6,1-22: a
│ number is required.
╵

OpenTofu stops before planning anything. Text that reads as a number is a different case: change the file to book_price = "18.00" and OpenTofu converts it:

TerminalBash
tofu plan
Output (the lines that matter)Plain text
Changes to Outputs:
  ~ book_price     = 14.99 -> 18

You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.

Clean Up

Run tofu destroy and type yes to remove the saved outputs, then delete the folder.

Common Mistakes

MistakeWhat happensDo this instead
Using list for values that must be uniqueDuplicates stay in the list.Use set.
Using map(string) for values of different typesEvery value is turned into text, so numbers lose their type.Use object({…}) with a type for each attribute.

FAQ

Is a type required?

No, but the docs recommend it: it documents the variable and gives a clear error when a value is wrong.

Why did 18.00 become 18?

OpenTofu converted the text "18.00" to the number 18, and prints numbers without trailing zeros. See Types and Values (opens in a new tab) for every conversion.

Conclusion

Give every variable the narrowest type that fits: string, number and bool for single values, list, set and map for collections, and object for a group of named values.

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

0 comments