When you define variables and values in Terraform, each one has a data type: it indicates what kind of information it contains (a text, a number, a list…). Knowing the types helps you write correct code and understand the examples you find. It’s a simple topic and very similar to the types in any programming language.

Why types matter

Declaring the type of a variable has two advantages:

  1. Clarity: whoever reads the code knows what each variable expects.
  2. Validation: Terraform warns you if you pass a value of the wrong type (for example, a text where a number was expected), preventing errors.

Types are divided into simple (a single value) and complex (collections of values).

Simple types (primitives)

string — text

Any text, in double quotes.

variable "region" {
  type    = string
  default = "eu-west-1"
}

Examples of values: "hello", "t3.micro", "eu-west-1".

number — numbers

Numbers, integers or decimals, without quotes.

variable "cantidad_servidores" {
  type    = number
  default = 3
}

Examples: 3, 0, 2.5, 100.

bool — true or false

A logical value: true or false. Useful for enabling/disabling things.

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

Usage example: a bool variable can decide whether a resource is created or not, or whether an option is enabled. For example, “enable Multi-AZ in the database? true/false”.

Complex types (collections)

These group several values. They are very useful for real configurations.

list — an ordered list of values

A sequence of values of the same type, in order, in brackets [ ].

variable "zonas_disponibilidad" {
  type    = list(string)
  default = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
}

This is a list of texts. list(string) means “a list of strings”. You access its elements by position (starting at 0): the first is var.zonas_disponibilidad[0].

Real example: a list of the availability zones where you want to deploy (remember Chapter 3), or a list of ports to open.

map — key-value pairs

A collection of “key = value” pairs, in braces { }. Like a dictionary where you look up a value by its key.

variable "tipos_por_entorno" {
  type = map(string)
  default = {
    dev  = "t3.micro"
    prod = "m5.large"
  }
}

You access a value by its key: var.tipos_por_entorno["prod"] returns "m5.large".

Real example: assign a different instance size to each environment, or a set of tags for your resources.

object — a structure with defined fields

An object groups several values of different types under specific field names. It’s like a record with fields.

variable "config_servidor" {
  type = object({
    tipo       = string
    cantidad   = number
    monitoreo  = bool
  })
  default = {
    tipo      = "t3.micro"
    cantidad  = 2
    monitoreo = true
  }
}

You access a field with a dot: var.config_servidor.tipo returns "t3.micro".

Difference map vs object:

  • A map has free keys and all values of the same type.
  • An object has predefined fields with specific names and types (which can be different from each other).

Summary table

Type What it stores Example
string Text "eu-west-1"
number Number 3, 2.5
bool True/false true
list Ordered list, same type ["a", "b", "c"]
map Key-value pairs, same type { dev = "t3.micro" }
object Structure with defined fields { tipo = "x", num = 2 }

The any type and inference

  • If you’re not sure about the type, there’s any (accepts any type), but it’s better to be specific when you can.
  • If you don’t declare the type, Terraform often infers it from the default value. Even so, declaring the type is good practice because it documents and validates.

A realistic example combining types

variable "config_red" {
  type = object({
    cidr        = string
    azs         = list(string)
    nat_activo  = bool
    tags        = map(string)
  })
  default = {
    cidr       = "10.0.0.0/16"
    azs        = ["eu-west-1a", "eu-west-1b"]
    nat_activo = true
    tags = {
      Proyecto = "tienda"
      Entorno  = "dev"
    }
  }
}

Here a single object describes the entire configuration of a network: its range (string), its zones (list), whether it has NAT (bool), and its tags (map). This is very similar to how things are configured in real projects.

What you should remember

  • Every value in Terraform has a type. Declaring it provides clarity and validation.
  • Simple types: string (text), number (numbers), bool (true/false).
  • Complex types: list (ordered list of the same type), map (key-value pairs of the same type), object (structure with defined fields of different types).
  • map vs object: map has free keys and values of the same type; object has predefined fields with specific types.
  • Declaring types is good practice, even though Terraform sometimes infers them.

In the next subchapter we’ll see how to work with these values using expressions, references, and built-in Terraform functions.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved