Variables are one of the most important concepts in Terraform. Day 5 of the 30 Days AWS Terraform challenge focuses entirely on understanding what variables are, why we need them, and how they make Terraform configurations reusable, maintainable, and safe.
This lesson finally answers why hardcoding must be avoided and shows how variables help control environments like dev, stage, and production without rewriting code.
🌱 Why Do We Need Variables?
Hardcoding values such as: AWS region VPC CIDR Tags Bucket names AMI IDs …causes repetition and errors.
A single typo can break your infrastructure. Variables solve this by: Centralizing values in one place Reducing duplication Allowing environment switching Preventing human error Improving long-term maintainability
🧩 Types of Vari…
Variables are one of the most important concepts in Terraform. Day 5 of the 30 Days AWS Terraform challenge focuses entirely on understanding what variables are, why we need them, and how they make Terraform configurations reusable, maintainable, and safe.
This lesson finally answers why hardcoding must be avoided and shows how variables help control environments like dev, stage, and production without rewriting code.
🌱 Why Do We Need Variables?
Hardcoding values such as: AWS region VPC CIDR Tags Bucket names AMI IDs …causes repetition and errors.
A single typo can break your infrastructure. Variables solve this by: Centralizing values in one place Reducing duplication Allowing environment switching Preventing human error Improving long-term maintainability
🧩 Types of Variables in Terraform
Terraform supports 3 major types of variables:
#🔸 1. Input Variables
Provide user-defined values to Terraform. Example: variable "env" { type = string default = "dev" }
Use it: tags = { Environment = var.env }
Input variables help eliminate repeated hardcoded values.
🔸 2. Local Values (locals)
Used for computed or reusable internal expressions. Example: locals { bucket_name = "${var.env}-myapp-bucket" } Then use it: bucket = local.bucket_name Locals help maintain consistent naming conventions and avoid long expressions.
🔸 3. Output Variables
Display useful values after applying infrastructure. Example: output "vpc_id" { value = aws_vpc.main.id }
Useful for: Debugging Passing values to other modules Automation
🧠 Variable Types
Terraform supports: string number bool list() set() map() object() tuple() Special: any, null Type constraints improve safety and catch errors early.
🧮 String Interpolation
Old syntax: ${var.env}-vpc New recommended syntax: "${var.env}-vpc" Interpolation allows dynamic naming, concatenation, and more.
🔀 Variable Precedence — Which Value Wins?
Terraform chooses variable values in the following order (highest → lowest):
1️⃣ Command line terraform apply -var="env=stage"
2️⃣ .tfvars file Example: dev.tfvars env = "dev"
3️⃣ Environment variables export TF_VAR_env="prod"
4️⃣ Default values (inside variable block)
Understanding precedence allows flexible environment switching without editing code.
🚀 Hands-On: Using Variables While Deploying Resources
Day 5 showed real provisioning with variables: Creating resources (e.g., VPC, S3 buckets) Fixing AMI and permission errors Updating variable-driven names
Running: terraform plan terraform apply terraform output Outputs displayed meaningful resource attributes, such as IDs.
🧰 Helpful Terraform State & Output Commands
View outputs: terraform output Useful for referencing values externally (pipelines, scripts, modules).
🏁 Conclusion
Day 5 introduced the core of writing clean Terraform code: Variables. By mastering input variables, locals, outputs, and precedence rules, Terraform configurations become:
✔ Reusable ✔ Scalable ✔ Easier to maintain ✔ Environment-ready ✔ Less error-prone _ Variables turn Terraform from “working code” into professional Infrastructure-as-Code._