Skip to main content

Command Palette

Search for a command to run...

Day 5 — Terraform Variables

Updated
1 min readView as Markdown
Day 5 — Terraform Variables
S

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-file

  • TF_VAR_<name>

  • default value

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, bool

  • Complex: list, set, map, object, tuple

  • Special: null (absence of value)

Variable Precedence (High → Low)

  1. -var / -var-file

  2. *.auto.tfvars

  3. terraform.tfvars

  4. TF_VAR_<name>

  5. default value

Last applied value wins.

Must Remember

  • Always define type

  • Use locals to avoid repetition

  • Never 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