Default values in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using default values in Terraform affects the time it takes to apply configurations.
Specifically, how does the number of default values impact the work Terraform does?
Analyze the time complexity of setting default values in variables.
variable "instance_count" {
type = number
default = 3
}
variable "instance_type" {
type = string
default = "t2.micro"
}
resource "aws_instance" "example" {
count = var.instance_count
instance_type = var.instance_type
ami = "ami-123456"
}
This code sets default values for variables and creates multiple instances based on the count.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating each AWS instance resource.
- How many times: Equal to the value of
instance_count, here 3 by default.
As the default count increases, the number of instances created grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 instance creations |
| 100 | 100 instance creations |
| 1000 | 1000 instance creations |
Pattern observation: The work grows directly with the number of instances specified by the default value.
Time Complexity: O(n)
This means the time to apply grows linearly with the number of instances created from the default value.
[X] Wrong: "Default values do not affect the number of resources created or time taken."
[OK] Correct: The default value for count directly controls how many resources Terraform creates, so it impacts the total work.
Understanding how default values influence resource creation helps you predict deployment time and cost, a useful skill in real cloud projects.
"What if we changed the default count to a variable input at runtime? How would the time complexity change?"
Practice
default value for a variable in Terraform?Solution
Step 1: Understand variable defaults in Terraform
Default values act as fallback values when no input is provided by the user.Step 2: Identify the purpose of default values
They make variables optional and provide a safe, predictable value.Final Answer:
To provide a fallback value if no other value is given -> Option BQuick Check:
Default value = fallback [OK]
- Thinking default makes variable mandatory
- Assuming default disables the variable
- Believing default auto-generates random values
region in Terraform?Solution
Step 1: Recall Terraform variable block syntax
Variables are declared with the keywordvariableand a name in quotes.Step 2: Check correct default assignment
The default value is set inside the block withdefault = "value".Final Answer:
variable "region" { default = "us-east-1" } -> Option AQuick Check:
Correct syntax uses variable block and default = value [OK]
- Missing quotes around variable name
- Using colon instead of equals for default
- Assigning default outside variable block
variable "instance_type" {
default = "t2.micro"
}What will be the value of
var.instance_type if no override is provided?Solution
Step 1: Identify the default value in the variable block
The variableinstance_typehas a default set to "t2.micro".Step 2: Understand behavior when no override is given
If no value is provided, Terraform uses the default value automatically.Final Answer:
"t2.micro" -> Option DQuick Check:
Default used if no override [OK]
- Assuming null if no value given
- Expecting error without override
- Thinking default is empty string
variable "env" {
default: "production"
}Why does Terraform give a syntax error?
Solution
Step 1: Check the syntax for default assignment
Terraform requires=to assign default values, not a colon.Step 2: Identify the cause of syntax error
Usingdefault: "production"causes a syntax error because colon is invalid here.Final Answer:
Because default uses colon instead of equals sign -> Option AQuick Check:
Use = not : for default [OK]
- Using colon instead of equals for default
- Removing quotes from variable name
- Thinking default must be numeric
availability_zones with a default list of zones ["us-east-1a", "us-east-1b"]. Which is the correct way to declare it in Terraform?Solution
Step 1: Define variable with correct type for list of strings
Usetype = list(string)to specify a list of strings.Step 2: Assign default as a list of strings
Setdefault = ["us-east-1a", "us-east-1b"]inside the variable block.Final Answer:
variable "availability_zones" { type = list(string) default = ["us-east-1a", "us-east-1b"] } -> Option CQuick Check:
List type with list default [OK]
- Omitting type for list variables
- Assigning list to string type
- Using string with commas instead of list
