variable blocks and how to use input variables to make your infrastructure code flexible, reusable, and environment-aware. You’ll learn why hard-coded values are problematic, how variables solve those problems, and the anatomy of a variable block so you can parameterize your Terraform modules effectively.
The problem with hard-coded values
Managing multiple environments (production, QA, development) often leads to nearly identical resource blocks that differ only by values. Hard-coding those values causes duplication, increases maintenance effort, and makes configuration drift likely. Example of duplicated, hard-coded resources:location or address_space require many edits, increasing the chance of errors and drift.
How variables solve the problem
Parameterize configuration values intovariable blocks and centralize environment-specific values in .tfvars files or CI/CD pipeline inputs. A single Terraform module becomes the shared, validated source of truth; teams and environments only supply different values.

- Consistency: All environments use the same, tested configuration.
- Collaboration: Teams provide variable files (e.g.,
prod.tfvars,dev.tfvars) without touching shared modules. - Maintainability: One change in the core module propagates across environments.
- Scalability: To add an environment, create one variable file—no template copying.
var.*:
terraform.tfvarsor*.tfvarsfiles (e.g.,prod.tfvars,dev.tfvars)- Environment-specific
.tfvarsfiles referenced by-var-file -varCLI flags (for ad-hoc overrides)- CI/CD variable injections or remote state-based workflows
What is a variable block?
Avariable block declares an input to the module or root module. It defines a named parameter and optional metadata:
description: human-readable texttype: input type (validates input values)default: fallback value when no value is provided
type, Terraform will attempt to infer it from a default or provided value, but explicit types catch errors earlier.

If you omit
type, Terraform will try to infer it from a default or from input values; explicit types, however, help catch configuration errors early.Anatomy of a variable block
| Element | Purpose | Example |
|---|---|---|
| Name | Identifier used as var.<name> in code | variable "rg_name" {} |
| Description | Documents intent for maintainers | description = "Resource group name" |
| Type | Validates shape and primitive types | type = string, type = list(string), type = map(number) |
| Default | Fallback value if nothing is provided | default = "dev-resources" |
string, number, bool, list(<type>), map(<type>), object({...}), and any.
Example: variable definitions and use
variables.tfAvoid committing sensitive values into
.tfvars in version control. Use Terraform Cloud/Enterprise, Vault, environment variables, or CI/CD secret stores to manage secrets securely.Quick reference — how to provide variable values
| Method | Precedence | Typical use |
|---|---|---|
-var CLI flag | Highest | Temporary overrides |
-var-file | High | Environment-specific files in CI/CD |
Environment variable TF_VAR_<name> | Medium | CI agents or scripts |
terraform.tfvars / *.tfvars | Lower | Local defaults for developers |
default in variable block | Lowest | Module fallback |
Summary
Input variables let you parameterize Terraform modules so a single, shared configuration can be safely reused across environments. Define the contract invariable blocks (name, description, type, default) and supply environment-specific values via .tfvars, CLI flags, or pipeline variables. This reduces duplication, improves maintainability, and minimizes configuration drift.
Next steps:
- Explore variable types in detail (
object,map,tuple) - Learn patterns for structuring
.tfvarsper environment - Investigate best practices for handling sensitive data with Vault or Terraform Cloud
Links and references
- Terraform Input Variables
- Terraform CLI: Specifying Variables
- HashiCorp Best Practices for Variables