Skip to main content
This lesson explains how to define and use input variables in Terraform to make your configurations reusable and environment-agnostic. Defining variables means declaring named inputs that Terraform configuration can reference instead of hardcoded values. Variables act as parameters, enabling the same Terraform code to be reused across environments (dev, staging, prod) by changing only the input values. Hardcoded resource example Below is an Azure Storage Account resource where key configuration values are hardcoded directly in the resource block. This will deploy successfully, but it tightly couples the resource to a single name, region, and resource group. Any change (for example, deploying to a different region) requires editing the Terraform code.
Why this is brittle
  • Hardcoded values prevent easy reuse across environments.
  • Editing multiple resource blocks for a simple change is error-prone.
  • Sharing or versioning configurations becomes harder.
Use variables to decouple values from resource blocks and centralize configuration. Declaring and using variables Create input variables in a variables.tf file and reference them using var.<name> from your resource files (main.tf, etc.). Variables declaration (variables.tf):
Use the variables in your resource (main.tf):
Referencing var.location and var.resource_group_name allows multiple resources to reuse the same inputs. If the region or resource group needs to change, update the variable defaults or supply different values at runtime in one place instead of editing every resource block.
Variables can be provided by default values, environment variables, a .tfvars file, or via CLI flags when you run Terraform. This makes configurations portable and easier to manage across environments.
Common ways to provide variable values Demonstration: creating variables and resources in a project directory
  • We’ll create a small Terraform project that defines a provider, variables, and a single resource group resource.
  • For brevity, this demo uses simpler variable names (rg and region) to illustrate the same concept.
Provider configuration (provider.tf):
Declare variables (variables.tf):
Use variables in the resource group resource (main.tf):
Initialize the working directory and inspect the plan:
Sample terraform init output:
Sample terraform plan output:
Note that the plan shows the resolved values (for example, eastus and kodekloud-tf-var-rg) rather than var.rg or var.region. This makes it easy to review what Terraform will deploy.
Do not store sensitive secrets (API keys, passwords) in plaintext variables.tf defaults or checked-in .tfvars files. Use environment variables, Terraform Cloud/Enterprise workspace variables, or secret management integrations to protect sensitive data.
Quick reference: variable block essentials Summary
  • Declaring variables decouples configuration values from resource blocks, improving reusability and maintainability.
  • Use var.<name> in resource blocks to reference declared variables.
  • Variable values can be supplied via defaults, environment variables (TF_VAR_), .tfvars files, or CLI -var flags.
  • For secrets, avoid checked-in defaults and prefer secure secret mechanisms.
Further reading and references A deeper dive into the variable block would cover advanced topics such as type constraints, validation blocks, sensitive attributes, and multiple ways to provide variable values across CI/CD and team workflows.

Watch Video