Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
📘 Terraform Default Values: Providing Fallback Values for Variables
When writing Terraform code, we often rely on variables to make our infrastructure flexible. Variables allow us to define dynamic configurations instead of hardcoding values. But what happens if a variable is not supplied by the user?
👉 This is where Terraform Default Values come in.
Default values act as “fallbacks.” If no value is provided during runtime, Terraform will automatically use the default. This ensures that Terraform configurations remain reliable, predictable, and user-friendly.
Think of default values like a backup plan — if no one gives you instructions, Terraform already knows what to do.
🔑 What Are Terraform Default Values?
In Terraform, when you define a variable, you can optionally assign a default value.
- If the user does not provide a value, Terraform uses the default.
- If the user does provide a value, it overrides the default.
Basic Syntax:
variable "region" { type = string default = "us-east-1"}
- If the user doesn’t pass a region →
us-east-1
will be used. - If the user passes a value (e.g.,
us-west-2
) → Terraform uses that instead.
🖥️ 3 Example Programs for Terraform Default Values
✅ Example 1: Default AWS Region
variable "region" { type = string default = "us-east-1"}
provider "aws" { region = var.region}
resource "aws_s3_bucket" "default_bucket" { bucket = "default-value-example"}
🔍 Explanation
- If the user doesn’t specify a
region
, Terraform uses"us-east-1"
. - If a region is provided (via CLI or
.tfvars
), it overrides the default.
✅ Example 2: Default Instance Type
variable "instance_type" { type = string default = "t2.micro"}
resource "aws_instance" "server" { ami = "ami-0c55b159cbfafe1f0" instance_type = var.instance_type}
🔍 Explanation
- Default instance type is t2.micro.
- If someone wants a larger server, they can override it.
- Great for cost control in dev/test environments.
✅ Example 3: Default Number of Instances
variable "instance_count" { type = number default = 1}
resource "aws_instance" "web" { count = var.instance_count ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro"}
🔍 Explanation
- Default number of instances is
1
. - In production, the user can override with
3
or5
. - Helps scale deployments flexibly.
🧩 Advantages of Using Default Values
- Reliability – Terraform won’t break if the user forgets to supply a value.
- Flexibility – Users can still override defaults when needed.
- Simplicity – Makes Terraform code easier for beginners and teams.
- Reusability – Same code works in multiple environments without edits.
- Best Practices – Industry standard for Infrastructure as Code projects.
📖 Real-World Use Cases
- Cloud Region Selection – Default to a primary region, override when deploying elsewhere.
- Instance Types – Default to cost-friendly machines, override for production workloads.
- Scaling – Default to minimal infrastructure in dev, scale up in prod.
- Tagging – Default to basic tags (e.g., Owner=DevTeam), override for specific projects.
- Network Config – Default subnets for staging, override for production.
🧠 How to Remember Terraform Default Values (Exam & Interview Prep)
Here’s a memory trick:
👉 “Fallback First” Rule
Always think: If no input is given, what’s the fallback? That’s the default value.
Another mnemonic: D = Default, D = Backup
- Default value = Backup plan
In interviews, you can phrase it like this: “Terraform default values ensure that configurations remain functional even if users forget to pass variable values. They act as fallback values.”
🎯 Why Is It Important to Learn Default Values?
- For Beginners: Makes Terraform less intimidating because they don’t need to provide every variable.
- For Teams: Ensures consistency across environments.
- For Exams: Default values are frequently asked in Terraform Associate certification.
- For Real Projects: Prevents failures when variables are missing.
Imagine deploying infrastructure at midnight, and you forgot to pass a variable. Without defaults, the deployment fails. With defaults, it works smoothly. 🚀
📊 Comparison: Without vs With Default Values
Feature | Without Default Value | With Default Value |
---|---|---|
Input Required | Mandatory | Optional |
Flexibility | Low | High |
Beginner Friendly | No (must provide values) | Yes (works without input) |
Fail-Safe | Breaks without input | Always has a fallback |
Example | terraform apply -var=region=us-west-2 | terraform apply (works by default) |
🖥️ Additional Examples
✅ Example 4: Default Tags
variable "tags" { type = map(string) default = { Environment = "dev" Owner = "team1" }}
resource "aws_s3_bucket" "bucket" { bucket = "default-tag-bucket" tags = var.tags}
Explanation:
- Default tags ensure consistency.
- Override tags for production with
terraform.tfvars
.
✅ Example 5: Default Subnets
variable "subnets" { type = list(string) default = ["subnet-123", "subnet-456"]}
resource "aws_instance" "server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" subnet_id = var.subnets[0]}
Explanation:
- Default subnets help with dev testing.
- Users can override with production subnets.
✅ Example 6: Default Database Configuration
variable "db_config" { type = object({ engine = string version = string storage = number }) default = { engine = "mysql" version = "8.0" storage = 20 }}
resource "aws_db_instance" "default_db" { engine = var.db_config.engine engine_version = var.db_config.version allocated_storage = var.db_config.storage instance_class = "db.t3.micro"}
Explanation:
- Provides a ready-to-use DB setup without input.
- Can be overridden in production for scaling.
🧠 Interview & Exam Preparation Tips
-
Definition to Memorize: “Terraform default values are fallback values assigned to variables, used when no user input is provided.”
-
Common Question: Q: What happens if a variable has no default value and the user doesn’t supply it? A: Terraform will prompt the user for input.
-
Hands-On Practice: Always try writing at least one Terraform project with and without default values to see the difference.
🏆 Conclusion
Terraform Default Values are like a safety net. They make Terraform code:
- Reliable
- Flexible
- Easy to use
By using default values, you can write Terraform modules that work out-of-the-box while still being customizable.
👉 Key Takeaway: Always set default values for non-critical variables (like region, tags, or instance type). Leave critical ones (like passwords) without defaults to enforce explicit input.