Language

Variables & State · Lesson 9 of 30

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

  1. Read simple section
  2. Read analogy + diagram
  3. Copy project files
  4. 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)

  1. default in variables.tf — automatic
  2. terraform.tfvars file — your saved answers per project
  3. command lineterraform 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)

TypeMeaningExample value
stringText"ap-south-1"
numberCount3
boolYes/notrue
list(...)List of items["a","b"]
map(...)Dictionary{ size = "t3.micro" }

Common beginner mistakes

  • Writing environment instead of var.environment inside 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

Example HCL
HCL
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 below
terraform {
  required_version = ">= 1.9.0"
}