terraform plan and terraform apply, reducing runtime surprises.
The three primitive types are:
stringnumberbool
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. Usenumber 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:
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 at0).
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"]
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: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:- 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 thevar prefix.
- Single-value variables:
var.vsphere_datacenter - List elements by index:
var.vsphere_networks[0] - Map entries by key:
var.course_details["instructor"]orvar.course_details.instructor
Best practices
- Prefer explicit type constraints (e.g.,
list(string),map(string),set(string)) for clearer validation and better error messages. - Use
object({...})ortuple([...])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 nodefaultto force consumer input. - Document variable purpose with
descriptionand examples; includevalidationblocks for advanced constraints.
Summary
- Primitive types (
string,number,bool) are the simplest building blocks and provide early validation. listis an ordered sequence accessed by index.mapis a key/value collection accessed by key and is unordered.setguarantees uniqueness and is unordered; convert tolistwhen you need indexing or ordering.- Always reference variables using
var.<name>and prefer explicit type constraints for clearer validation and better error messages.
Links and References
- Terraform Documentation — Input Variables
- Terraform Documentation — Types
- HCL (HashiCorp Configuration Language) Overview