linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Knowledge base
  • Cheat sheet
  • Capstone
  • Interview prep
home/terraform/kb/Variables & outputs

kb/variables

Variables, outputs, and locals in Terraform

Terraform variables, outputs, and locals: a variable block takes input (CLI -var, env TF_VAR_*, .tfvars, default). An output block exposes values for reuse elsewhere. Locals are computed internal names. This is the grammar of any config beyond hello-world.

  • tf-tfvars.tfvars: variable value files

    .tfvars files hold variable values in HCL or JSON format. terraform.tfvars and *.auto.tfvars are loaded automatically; all others require -var-file. This is the main mechanism for separating code from environment configuration.

  • tf-localslocals: computed internal names

    locals is a block with names visible only inside HCL (not input, not output). Useful for DRY: compute a common prefix or tag set once, then use it everywhere via local.x. Do not confuse with variable (input) and output (output).

  • tf-outputThe output block: what Terraform exposes to the outside

    An output is a value that Terraform displays after apply and stores in state. Use it to (a) show the user the ID or ARN of a created resource, (b) pass values between modules, or (c) feed values to scripts via `terraform output -raw`.

  • tf-variableThe variable block: input to your configuration

    A variable is a parameter that receives its value from outside the configuration (CLI, environment variable, .tfvars file). You declare it in HCL with type, default, description, and validation, then reference it as var.name. Variables remove hardcoded values and let one HCL configuration serve multiple environments.

  • tf-variable-sourcesWhere Terraform reads variable values from

    Six sources, in priority order: -var in CLI > -var-file > *.auto.tfvars (alphabetically) > terraform.tfvars > TF_VAR_* env > default. If none of these supply a value, Terraform prompts interactively. Understanding this order matters for CI/CD.

Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies