Skip to main content
In this lesson we’ll cover Terraform variable types — the primitive types plus the commonly used complex collection types that you’ll use to parameterize configurations. Explicit types improve validation during terraform plan and terraform apply, reducing runtime surprises. The three primitive types are:
  • string
  • number
  • bool
Each type enforces a simple contract for values passed into modules and configurations, helping Terraform catch invalid inputs early.
Declaring explicit types helps Terraform validate inputs early (during plan/apply), preventing common mistakes like passing the string "three" where a numeric 3 is required.

Primitive types

Strings are sequences of characters. Typical uses: resource names, region codes, tags, DNS names and other textual identifiers. Numbers represent integers or floating-point values. Use number for counts, sizes, timeouts and numeric configuration values — Terraform validates numeric-ness. Booleans (bool) are true/false switches used to enable or disable features (for example, high availability, monitoring, or public access). Example declarations:
If someone tries to set num_of_vms to the text "three", Terraform will raise a type error because the variable expects a numeric value.

Collection types: lists, maps, sets

This section covers the common collection types: list, map, and set. Use list when order/position matters, map to associate keys with values for readability, and set when uniqueness matters and order does not.
Note: If you place examples that include curly braces in table cells, wrap them in backticks (example above) so MDX/HCL does not try to parse them as JavaScript or markup.

Lists

Lists store an ordered sequence of values. Use lists when you need positional access to items (indexes start at 0). Common uses:
  • allowed instance types: ["t3.small", "t4g.micro"]
  • availability zones: ["us-east-1a", "us-east-1b"]
  • subnet CIDR blocks: ["10.1.0.0/24", "10.2.0.0/24"]
Example:
Access by index (var.permitted_size[0] for the first element). Attempting to access an index that doesn’t exist will result in an error, so validate or guard against out-of-range indices.

Maps

Maps associate keys to values (key/value pairs). They are great for self-documenting configuration and let you access values by meaningful keys instead of numeric indexes. Example:
Access map values using either bracket syntax or dot notation when the key is a valid identifier:
Maps are unordered — do not rely on any specific ordering of key/value pairs.

Sets

Sets are collections of unique values where order does not matter. Use sets when duplicates are invalid (e.g., subnet IDs, IP addresses, domain names). Example:
Important properties of sets:
  • Values are unique — Terraform will deduplicate automatically.
  • There is no positional indexing for sets because they are unordered.
  • Convert a set to a list when you need indexing or an iteration order:
Use sets when you only care about uniqueness and not order. Convert to a list when you need indexing or ordered iteration, but document that the resulting order might be arbitrary unless you sort it (e.g., sort(tolist(var.some_set))).

Referencing variables in configuration

Reference variables with the var prefix.
  • Single-value variables: var.vsphere_datacenter
  • List elements by index: var.vsphere_networks[0]
  • Map entries by key: var.course_details["instructor"] or var.course_details.instructor
Example definitions:
Then reference them:
Remember that lists index from zero. Always validate indices exist or guard access to avoid out-of-range errors.

Best practices

  • Prefer explicit type constraints (e.g., list(string), map(string), set(string)) for clearer validation and better error messages.
  • Use object({...}) or tuple([...]) when you need structured records with known fields and types (useful for module inputs). Define these explicitly to improve readability and validation.
  • Default sensible values when appropriate, but avoid hiding required inputs; prefer variable "x" { type = string } with no default to force consumer input.
  • Document variable purpose with description and examples; include validation blocks for advanced constraints.

Summary

  • Primitive types (string, number, bool) are the simplest building blocks and provide early validation.
  • list is an ordered sequence accessed by index.
  • map is a key/value collection accessed by key and is unordered.
  • set guarantees uniqueness and is unordered; convert to list when you need indexing or ordering.
  • Always reference variables using var.<name> and prefer explicit type constraints for clearer validation and better error messages.

Watch Video