Variables & Outputs
Source: doonops-curriculum/variables-state--input-output-vars.md
Doonops lesson
Goal
Variables and outputs — the #1 beginner topic. Learn like form fields, not magic.
Simple explanation
variable = question on a form ("What size server?"). var.name = read the answer. output = result you show after work ("Here is the website URL").
Technical view
Input variables with type, default, validation; var. reference; output values exposed after apply; tfvars files per environment; sensitive flag.
Think of it like
Pizza order form: variable = crust size choice. tfvars = your saved usual order. output = receipt with order number.
Steps
- Read simple section
- Read analogy + diagram
- Copy project files
- Do local lab
Deep explanation
Layman words first, then technical detail — read slowly
Variable — simplest definition
A variable is an empty slot you fill in later so one code works for dev and prod without editing main.tf every time.
variable "environment" {
type = string
default = "dev" ← if nobody passes a value, use "dev"
}
resource "aws_instance" "app" {
tags = { Env = var.environment } ← var. = read the variable
}
Layman: var.environment means "whatever word the user chose for environment".
Where values come from (3 ways)
- default in variables.tf — automatic
- terraform.tfvars file — your saved answers per project
- command line —
terraform apply -var="environment=staging"
Output — simplest definition
After apply, Terraform can print/share useful values.
output "instance_id" {
value = aws_instance.app.id ← pipe result out to human or other code
}
Layman: Output = "show me the answer" after cooking — IP address, load balancer DNS, etc.
Types (beginner table)
| Type | Meaning | Example value |
|---|---|---|
| string | Text | "ap-south-1" |
| number | Count | 3 |
| bool | Yes/no | true |
| list(...) | List of items | ["a","b"] |
| map(...) | Dictionary | { size = "t3.micro" } |
Common beginner mistakes
- Writing
environmentinstead ofvar.environmentinside resources - Putting secrets in tfvars and pushing to GitHub — use env vars or AWS Secrets Manager later
- Expecting output to exist before apply — outputs appear after resources exist
Example (Doonops)
Modern HCL — names are examples, not from any third-party course
variable "environment" {
type = string
default = "dev"
}
output "env_label" {
value = var.environment
}Terraform runs on your computer — copy this HCL into a folder, then follow the local lab steps below.
Check
- var. kyun lagta hai
- tfvars vs default
- output kab milta hai
Project files for this lab
Full implementation folder — copy all files, then run terraform commands
Lab project files (full folder)
Copy every file below into one folder — same as a real repo module. Then run the local lab steps.
Suggested folder: Suggested path: ~/doonops-terraform/06-variables/
versions.tfSee file purpose in the code belowterraform {
required_version = ">= 1.9.0"
}