Day 5 — Terraform Variables

DevOps Enginner
1. Variables Based on Purpose (Most Important)
1️⃣ Input Variables
Used to pass values into Terraform.
Avoid hardcoding
Make configs reusable
Can be mandatory or optional (via
default)
Common sources:
terraform.tfvars*.auto.tfvars-var/-var-fileTF_VAR_<name>defaultvalue
2️⃣ Local Variables
Used for derived values and reuse inside a module.
Reduce duplication
Improve readability
Cannot be overridden
Best for naming conventions and expressions.
3️⃣ Output Variables
Expose values after terraform apply.
Share values across modules
Show important info (IP, IDs)
Can be marked
sensitive
2. Variables Based on Values (High Level)
Primitive:
string,number,boolComplex:
list,set,map,object,tupleSpecial:
null(absence of value)
Variable Precedence (High → Low)
-var/-var-file*.auto.tfvarsterraform.tfvarsTF_VAR_<name>defaultvalue
Last applied value wins.
Must Remember
Always define
typeUse
localsto avoid repetitionNever hardcode secrets
Mark sensitive outputs
Keep variables organized (
variables.tf,outputs.tf)

For more detail check Github Code:- https://github.com/sidharthhhh/terraform/tree/main/day5
Check this vedio for more clearity:- https://youtu.be/V-2yC39BONc?si=KAjKH7EFhK0BsV3P



