Skip to main content

Variable Types

Imagine you’re designing a control panel for your Lego city. Each knob or switch controls something different: one sets the number of houses, another toggles streetlights on or off, another chooses the color of roofs.

In Terraform, variable types are those knobs and switches. They define what kind of values your variables can hold - whether it’s a simple string, a number, a true/false flag, or even a complex map of settings. By mastering variable types, you gain precision and flexibility in your infrastructure code.


String Variables

  • Represent text values.
  • Use case: Cloud region names, instance types, resource tags.
variable "region" {
  type    = string
  default = "us-east-1"
}

Number Variables

  • Represent numeric values.
  • Use case: Number of servers, storage size, scaling parameters.
variable "instance_count" {
  type    = number
  default = 2
}

Boolean Variables

  • Represent true/false values.
  • Use case: Feature toggles (enable logging, enable backups).
variable "enable_monitoring" {
  type    = bool
  default = true
}

List Variables

  • Represent ordered collections of values.
  • Use case: Distributing resources across multiple zones.
variable "availability_zones" {
  type    = list(string)
  default = ["us-east-1a", "us-east-1b"]
}

Map Variables

  • Represent key‑value pairs.
  • Use case: Environment‑specific configurations.
variable "instance_types" {
  type = map(string)
  default = {
    dev  = "t2.micro"
    prod = "t2.large"
  }
}

Complex Types

  • Terraform also supports object and tuple types for advanced use cases.
  • Use case: Grouping multiple attributes into one variable.
variable "server_config" {
  type = object({
    name = string
    size = number
    tags = list(string)
  })
}

Hands‑On Lab / Demo

Lab: Experimenting with Variable Types

  1. Run terraform plan → see how variables influence resource creation.

Reference them in main.tf:hcl

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_types["dev"]
  count         = var.instance_count
}

Create variables.tf with different types:

variable "region" {
  type    = string
  default = "us-east-1"
}

variable "instance_count" {
  type    = number
  default = 2
}

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

variable "availability_zones" {
  type    = list(string)
  default = ["us-east-1a", "us-east-1b"]
}

variable "instance_types" {
  type = map(string)
  default = {
    dev  = "t2.micro"
    prod = "t2.large"
  }
}

Pro Tips & Best Practices

  • Always specify variable types for clarity and validation.
  • Use maps for environment‑specific values.
  • Use lists for multi‑zone or multi‑region deployments.
  • Keep variable definitions in variables.tf for organization.
  • Document variables with description for team readability.

Summary & Cheatsheet

  • String: Text values.
  • Number: Numeric values.
  • Bool: True/false toggles.
  • List: Ordered collections.
  • Map: Key‑value pairs.
  • Advanced: Object, Tuple for complex structures.
Quick mnemonic: SNBLM → Strings, Numbers, Booleans, Lists, Maps

The Hackers Notebook

Variable types give Terraform structure and precision. They ensure your configurations are flexible yet predictable, allowing you to scale across environments with confidence. By mastering types, you’ve taken a big step toward writing professional, reusable Terraform code.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025