Lesson 4 of 4 ยท Introduction to Terraform

Variables and outputs

Variables are values you can change without rewriting a configuration, such as a region, project name, or network range. Outputs print useful values after Terraform finishes. They make a configuration easier to reuse and understand, regardless of the provider you choose.

Example

variable "environment" {
  default = "development"
}

output "selected_environment" {
  value = var.environment
}

Step by step

  1. Declare a variable.
  2. Read it with var.variable_name.
  3. Add an output for a value another person or system needs.
  4. Keep secrets out of default values and version control.

Common mistakes to avoid

  • Putting passwords directly in a .tf file.
  • Using a variable where you actually need a reference to a created resource.