- 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 adefault 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:
terraform plan without supplying values triggers an interactive prompt:
- Simple and immediate for ad-hoc runs or demos.
- 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:
- Quick for testing and one-off overrides.
- 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:
terraform.tfvarsis loaded automatically when you runterraform planorterraform apply.- Any file ending in
.auto.tfvarsis also auto-loaded. - JSON equivalents (
terraform.tfvars.json,*.auto.tfvars.json) are supported when valid JSON.
-var-file:
dev.tfvars, staging.tfvars, prod.tfvars).
Example plan output when using a var file:
- Keeps values out of Terraform source files.
- Easy to version-control non-sensitive defaults (but don’t commit secrets).
- Convenient for single primary environments.
- 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 namedTF_VAR_<variable_name> to the corresponding input variable.
Example:
- 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.
- Good for CI/CD secrets when your pipeline manager stores secrets securely.
- Not persisted in project files.
- 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):- Default value in
variabledeclaration. terraform.tfvars(auto-loaded).terraform.tfvars.json(auto-loaded).- Auto-loaded files ending in
.auto.tfvarsor.auto.tfvars.json(alphabetical order; later files override earlier ones). - Environment variables:
TF_VAR_*. - Command-line flags:
-varand-var-file(last flag wins if multiple).
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 withdescription and validation—helpful for interactive prompts and improving UX for other contributors.
terraform.tfvars file (one variable from tfvars, one from environment):
TF_VAR_region set in the environment and rg in terraform.tfvars, Terraform will use values from both sources:
Links and References
- Terraform: Variables documentation — https://www.terraform.io/language/values/variables
- Terraform: Input variable precedence — https://www.terraform.io/cli/config/variables
- Terraform: Variable files and JSON syntax — https://www.terraform.io/language/values/variables#variable-definitions-files