Skip to main content
A clear understanding of Terraform variable data types is essential for writing safe, maintainable infrastructure-as-code. Terraform is strongly typed: every input variable can — and usually should — have an explicit type. Picking the correct type improves validation, prevents runtime errors, and makes configurations easier to reason about as your infrastructure grows. This guide explains the common Terraform variable data types: what each represents, when to use it, and how to declare values in variables.tf and terraform.tfvars. It also includes a consolidated example showing how several types are used together in an azurerm_storage_account resource. For additional reading, see the Terraform docs on input variables: https://www.terraform.io/language/values/variables

Common Terraform variable data types

The image is a table detailing different variable datatypes including their descriptions, declaration methods, and example values. It covers types such as string, number, bool, list, set, map, object, tuple, and any.

Short notes on selected types

  • map: Use map(string) or map(number) when values share a single type; access entries via var.my_map["key"].
  • object: Use objects to group related attributes and access them with var.obj.attr.
  • tuple: Useful for fixed-position data such as [ "log", 30 ] where positions convey meaning.
  • any: Avoid in production code; it defeats type checking and can hide configuration errors.
Prefer specific types (string, number, bool, list, map, object) over any whenever possible. Strong typing enables Terraform to validate inputs and catch errors early.
The following diagram shows a concrete example of variable declarations for a storage account and how those variables are intended to be consumed by resources.
The image shows a code snippet from a Terraform configuration file defining variables for a storage account. It includes settings for the storage account name, HTTPS-only toggle, tags, and storage configuration.

Consolidated example

The example below demonstrates defining variables in variables.tf, consuming them in main.tf, and providing runtime values in terraform.tfvars.
Example terraform.tfvars (override defaults or provide runtime values):
Notes on the example:
  • storage_account_name is declared as a string.
  • https_only is a bool with a default of true; you can override it in terraform.tfvars.
  • tags is a map(string) used for tagging resources.
  • storage_config is an object grouping related attributes accessed via var.storage_config.location, var.storage_config.account_tier, etc.
Choosing the right data type enables Terraform to validate inputs, catch errors early, and keep your configurations clear and maintainable. Use objects to group related settings, maps for key-based lookups, lists/sets for collections (ordered or unique), and avoid any unless flexibility is essential.
Be cautious when using any. It disables type checking and can make debugging harder. Prefer explicit types for production code.
References

Watch Video

Practice Lab