Skip to main content
In this lesson we cover Terraform variables: how to declare them, how they make configurations reusable and environment-agnostic, and the common patterns for supplying values. Variables let you parameterize your Terraform configuration instead of hard-coding values. This makes code easier to reuse across environments (dev, staging, prod), simplifies CI/CD automation, and reduces duplication. Agenda for this lesson:
  1. Define input variables in Terraform
    • Externalize values such as region, resource names, SKUs, subscriptions, and environment-specific settings.
  2. Understand the variable block
    • Learn the structure and purpose of the variable block, including metadata such as type, default, and description. Note that the variable’s name is declared in the block header (e.g., variable "name" { ... }) rather than as a field inside the block.
  3. Supply values to variables
    • Explore ways to provide variable values: .tfvars files, command-line arguments (-var and -var-file), environment variables, and interactive prompts.
  4. Work with Terraform variable types
    • Review primitive types (string, number, bool) and complex types (list, map, object) with usage examples.
The image presents an agenda with four points about using input variables in Terraform configurations, including defining variables, understanding variable blocks, setting variable values, and identifying data types. The agenda is visually organized with numbered markers and a gradient background.
Use variables to decouple environment-specific data from configuration. This enables the same Terraform codebase to provision different environments by supplying different variable values.
We will now start by looking at how to declare input variables and the structure of a variable block.

Declaring input variables

A variable block defines an input variable for a module or root module. The block header contains the variable name; attributes inside define metadata and constraints. Basic example:
Key attributes:
  • description — human-readable context for the variable
  • type — the data type (e.g., string, number, bool, list(string), map(string), object({...}))
  • default — optional value used if none is supplied
  • sensitive — optional boolean to mark values as sensitive (prevents them from being shown in CLI output)
Example showing a mix of types, including map and object:
Referencing variables inside your configuration uses the var namespace:

Variable types — quick reference

When providing examples or inline values that include braces ({}) or double-curly syntax, wrap them in backticks to avoid MDX parsing issues. For example: `[{ "object": "person", "bbox": [0,0,10,10] }]`.

Ways to supply variable values

Terraform supports multiple ways to provide values for input variables. Choose the method that fits your workflow (local testing, automation, CI/CD, or secret management). Example terraform.tfvars:
Environment variable example:
CLI example:
Do not commit secrets (passwords, API keys) into .tfvars files stored in version control. Use a secrets manager or mark variables sensitive = true and supply values via secure pipelines or Terraform Cloud/Enterprise variable sets.

Best practices

  • Prefer tfvars files for environment-level defaults and -var-file to select them at runtime.
  • Use typed variables (list(...), map(...), object(...)) to validate inputs and catch mistakes early.
  • Document variable description values to aid collaborators and automation.
  • Avoid hard-coding environment-specific values inside modules—pass them as inputs to keep modules reusable.
We will next walk through concrete examples of using variables in modules and how to organize tfvars files per environment.

Watch Video