Variable declaration syntax in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to process variable declarations changes as we add more variables.
How does declaring more variables affect the work Terraform does?
Analyze the time complexity of declaring multiple variables in Terraform.
variable "name" {
type = string
default = "example"
}
variable "count" {
type = number
default = 3
}
variable "tags" {
type = map(string)
default = { env = "dev" }
}
This sequence declares three variables with different types and default values.
Each variable declaration triggers Terraform to register a variable and its metadata.
- Primary operation: Registering a variable with its type and default value.
- How many times: Once per variable declared.
As you add more variables, Terraform does more work registering each one.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 variable registrations |
| 100 | 100 variable registrations |
| 1000 | 1000 variable registrations |
Pattern observation: The work grows directly with the number of variables.
Time Complexity: O(n)
This means the time to process variables grows linearly as you add more variables.
[X] Wrong: "Declaring many variables happens instantly and does not affect processing time."
[OK] Correct: Each variable adds work because Terraform must read and store its details, so more variables mean more processing time.
Understanding how configuration size affects processing helps you design efficient infrastructure code and shows you think about scaling your work.
"What if variables had complex default values like large maps or lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand variable role in Terraform
Variables hold values that can be changed without editing the main code, making it flexible.Step 2: Compare with other options
Creating resources, defining outputs, or comments are different Terraform features, not variable declaration.Final Answer:
To store values that can be reused and customized in the configuration -> Option DQuick Check:
Variables = reusable values [OK]
- Confusing variables with resources
- Thinking variables define outputs
- Using variables as comments
region with a default value of us-west-1 in Terraform?Solution
Step 1: Recall Terraform variable declaration syntax
Variables use the block format: variable "name" { type = TYPE default = VALUE } with quotes around strings.Step 2: Check each option
variable "region" { type = string default = "us-west-1" } matches correct syntax with proper quotes and order. Options B and C are invalid syntax. variable "region" { default = us-west-1 type = string } misses quotes around default value.Final Answer:
variable "region" { type = string default = "us-west-1" } -> Option CQuick Check:
Correct block syntax with quotes [OK]
- Missing quotes around string values
- Using assignment (=) outside variable block
- Incorrect order or missing type keyword
variable "instance_count" {
type = number
default = 3
}What will be the value of
var.instance_count if no override is provided?Solution
Step 1: Identify default value in variable block
The variableinstance_counthas a default value set to 3.Step 2: Understand behavior when no override is given
If no value is provided during plan or apply, Terraform uses the default value.Final Answer:
3 -> Option AQuick Check:
Default value used when no override = 3 [OK]
- Assuming default is zero if not set
- Expecting error without override
- Confusing null with default
variable "enable_logging" {
type = bool
default = "true"
}Solution
Step 1: Check type and default value compatibility
The variable type is bool, so the default must be a boolean value, not a string.Step 2: Identify the error in default value
The default is "true" (a string), but it should be true (boolean without quotes).Final Answer:
The default value should not be in quotes for bool type -> Option AQuick Check:
Boolean default must be unquoted true/false [OK]
- Putting boolean default in quotes
- Confusing string and bool types
- Ignoring type mismatch errors
tags in Terraform that holds a map of strings with a default value of {"env" = "prod", "team" = "devops"}. Which is the correct declaration?Solution
Step 1: Understand map type syntax in Terraform
Maps of strings use type = map(string) and string values must be quoted.Step 2: Check default value format
Default must be a map with quoted string values inside curly braces, not square brackets.Step 3: Evaluate options
variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } correctly uses type = map(string) and default with quoted values. variable "tags" { type = map(string) default = { env = prod, team = devops } } misses quotes on values, variable "tags" { type = map default = { "env" = "prod", "team" = "devops" } } misses string type, variable "tags" { type = map(string) default = ["env" = "prod", "team" = "devops"] } uses square brackets (list) incorrectly.Final Answer:
variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } -> Option BQuick Check:
Map(string) with quoted string values in braces [OK]
- Missing quotes on map values
- Using map without specifying string type
- Using list syntax instead of map
