Skip to main content
Now that you understand what variables are and why they’re useful in Terraform, this guide shows how to provide concrete values at runtime. Declaring a variable in Terraform creates a placeholder; a value must be supplied by one of several supported methods. Below we walk through each method—from interactive prompts to production-ready CI/CD approaches—and explain precedence so there’s no confusion when multiple sources supply the same variable. Quick overview of methods:
  • Interactive prompts
  • Command-line flags (-var / -var-file)
  • Variable definition files (.tfvars / .auto.tfvars)
  • Environment variables (TF_VAR_*)

1) Interactive prompts

If a variable is declared without a default and no other source provides a value, Terraform prompts you at runtime. This is convenient for demos and learning but unsuitable for automation. Example declarations:
Running terraform plan without supplying values triggers an interactive prompt:
Pros:
  • Simple and immediate for ad-hoc runs or demos.
Cons:
  • Requires human input.
  • Not suitable for automated pipelines.

2) Command-line flags (-var)

You can provide variable values at runtime with the -var flag, which overrides values from files and defaults and bypasses interactive prompts. Example:
Terraform will then generate a plan immediately:
Pros:
  • Quick for testing and one-off overrides.
Cons:
  • Values appear in shell history.
  • Commands become long and error-prone.
  • Not ideal for secrets or repeatable workflows.

3) Variable definition files (tfvars)

Separating variable declarations from variable values is a common best practice. Place values in a .tfvars file:
Behavior:
  • terraform.tfvars is loaded automatically when you run terraform plan or terraform apply.
  • Any file ending in .auto.tfvars is also auto-loaded.
  • JSON equivalents (terraform.tfvars.json, *.auto.tfvars.json) are supported when valid JSON.
If you prefer explicit loading, use -var-file:
This is useful when switching environments manually (e.g., dev.tfvars, staging.tfvars, prod.tfvars). Example plan output when using a var file:
Pros:
  • Keeps values out of Terraform source files.
  • Easy to version-control non-sensitive defaults (but don’t commit secrets).
  • Convenient for single primary environments.
Cons:
  • Requires care to avoid committing secrets to VCS.

4) Environment variables (TF_VAR_*)

Environment variables are widely used in CI/CD to inject secrets and other runtime values without checking them into source control. Terraform maps any environment variable named TF_VAR_<variable_name> to the corresponding input variable. Example:
Notes:
  • Environment variables are often used for sensitive values because CI/CD systems provide secure ways to store them.
  • Treat any mechanism that exposes environment variables with care—environment values can leak into logs or process listings in some cases.
Pros:
  • Good for CI/CD secrets when your pipeline manager stores secrets securely.
  • Not persisted in project files.
Cons:
  • Potential exposure in logs or process listings if misused.

Variable precedence (lowest → highest)

Understanding precedence ensures you know which value Terraform will use when the same variable is defined in multiple places. Precedence (lowest to highest):
  1. Default value in variable declaration.
  2. terraform.tfvars (auto-loaded).
  3. terraform.tfvars.json (auto-loaded).
  4. Auto-loaded files ending in .auto.tfvars or .auto.tfvars.json (alphabetical order; later files override earlier ones).
  5. Environment variables: TF_VAR_*.
  6. Command-line flags: -var and -var-file (last flag wins if multiple).
For readability, here’s the same precedence in a compact table: Note: If a variable has no default and hasn’t been set by any of the above, Terraform will prompt you interactively as a last resort—unsuitable for automation.
Organize variables according to this hierarchy. Use environment variables for secrets in CI/CD, terraform.tfvars for project defaults, .auto.tfvars for per-environment variables, and -var/-var-file for temporary or manual overrides.

When to use each method

Use this quick reference to choose the right approach for your workflow: Terraform gives you flexible options; apply consistent conventions to scale and secure your deployments.

Example: prompts, env vars, and tfvars combined

This example demonstrates variable declarations with description and validation—helpful for interactive prompts and improving UX for other contributors.
Interactive prompt example:
Environment variable example:
Output (excerpt):
Using a terraform.tfvars file (one variable from tfvars, one from environment):
With TF_VAR_region set in the environment and rg in terraform.tfvars, Terraform will use values from both sources:
Plan output (excerpt):
Always remember the precedence rules when values are defined in multiple places. Use the simplest method that still scales and follow security best practices for secrets.

Watch Video