Terraform Tutorial · Terraform Language · Lesson 10 of 25

Terraform Types

Terraform's type system: strings, numbers, booleans, lists, sets, maps, objects and tuples, and how to declare them for variables.

Primitive and collection types

Type Example
string "eu-west-1"
number 3, 0.5
bool true
list(string) ["a", "b"] (ordered, duplicates allowed)
set(string) toset(["a", "b"]) (unordered, unique)
map(number) { small = 1, large = 4 }
object({...}) a fixed set of named attributes with their own types
tuple([...]) a fixed-length list with a type per position
A list of objects
variable "servers" {
  type = list(object({
    name = string
    size = string
  }))
  default = [
    { name = "web", size = "small" },
    { name = "db", size = "large" },
  ]
}

output "names" {
  value = [for s in var.servers : s.name]
}

Output

names = [
  "web",
  "db",
]

Run this on your computer — see “Get Started” for setup.

Optional object attributes

Since Terraform 1.3, object attributes can be optional, with defaults:

Optional attributes with defaults
variable "service" {
  type = object({
    name     = string
    port     = optional(number, 8080)
    replicas = optional(number, 1)
  })
  default = { name = "api" }
}

output "service" {
  value = var.service
}

Output

service = {
  "name" = "api"
  "port" = 8080
  "replicas" = 1
}

Run this on your computer — see “Get Started” for setup.

Test yourself

Which type keeps only unique values with no order?

  1. list
  2. set
  3. tuple
  4. map
Show answer

Answer: set