Site icon JD Bots

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

Syntax

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

<TYPE> is one of these:

The OpenTofu docs list these under Type Constraints.

Prerequisites

Step 1: Declare One Variable of Each Type

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

variables.tf

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.tf

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:

Terminal

tofu apply

Output

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"

Step 3: Give a Value of the Wrong Type

Create terraform.tfvars with text where a number belongs:

terraform.tfvars

book_price = "free"

Terminal

tofu plan

Output

│ 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:

Terminal

tofu plan

Output (the lines that matter)

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 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.

Related

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.

Exit mobile version