Hardcoding values directly inDocumentation Index
Fetch the complete documentation index at: https://gcore.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
main.tf works for a quick test but breaks down as soon as a project grows: credentials end up in source control, switching between environments requires editing source files, and configuration becomes hard to review. Terraform input variables solve all three problems by separating values from logic — secrets stay out of source files, environments switch by swapping a single .tfvars file, and the configuration itself stays readable.
Recommended project structure
Instead of one monolithicmain.tf, split configuration into purpose-specific files. Terraform loads all .tf files in a directory automatically, so the split is purely organizational.
terraform.tfvars often contains API keys and other secrets. Add it to .gitignore. Commit .terraform.lock.hcl to lock provider versions for the whole team.Provider configuration
Move the provider configuration out ofmain.tf into a dedicated providers.tf so it is easy to find and update:
api_key value now comes from a variable instead of being hardcoded.
Define variables
All variable declarations go invariables.tf. A variable block has four optional fields:
| Field | Purpose |
|---|---|
description | Documents what the variable is for. Shown when running terraform plan and in generated documentation. |
type | Constrains the accepted value type. Terraform rejects a wrong type before connecting to any API. |
default | Makes the variable optional. If omitted, Terraform requires a value to be supplied. |
sensitive | When true, masks the value in all command output and state display. |
Variable types
Terraform supports simple types and collection types.Simple types
Collection types
Sensitive variables
Mark variables that hold secrets withsensitive = true:
terraform.tfstate. Do not commit the state file to version control.
Input validation
Add avalidation block to catch bad values before Terraform makes any API call:
Assign values to variables
Terraform resolves variable values in this order (later sources override earlier ones):| Priority | Method | Best for |
|---|---|---|
| 1 (lowest) | default in variable block | Safe fallback values |
| 2 | terraform.tfvars | Local development |
| 3 | Named .tfvars file with -var-file | Multiple environments |
| 4 | -var flag | One-off overrides |
| 5 (highest) | TF_VAR_* environment variable | CI/CD pipelines, secrets managers |
Auto-loaded file
Createterraform.tfvars in the project directory. Terraform loads it automatically without any flags:
terraform plan — Terraform reads the file automatically:
Files per deployment stage
For separate environments (dev, staging, production), create one.tfvars file per environment:
staging.tfvars:
-var-file:
Command-line override
Override a single variable without editing any file:-var flags to override several variables at once:
-var flag takes the highest priority among file-based methods — it overrides values from .tfvars files.
CI/CD injection
Any variable namedexample can be set via the environment variable TF_VAR_example. This is the preferred method in CI/CD pipelines and secret managers where writing files is impractical.
- PowerShell
- Bash / Zsh
Define outputs
Outputs expose values from the Terraform state — useful for reading resource IDs after apply, or for passing values between configurations. All output declarations go inoutputs.tf:
terraform apply, outputs print automatically:
Inspect variables interactively
terraform console opens an interactive prompt that evaluates expressions against the current configuration and state. Use it to check variable values and test expressions before using them in resources.
exit or press Ctrl+D to close the console.
terraform console loads the configuration once at startup. After editing .tf files, exit and restart the console to see updated values..gitignore recommendations
.terraform.lock.hcl — it locks provider versions for the team and does not contain secrets.