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
- Declare a variable.
- Read it with var.variable_name.
- Add an output for a value another person or system needs.
- 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.