Module inputs (variables) in Terraform - Time & Space Complexity
We want to understand how the time to apply Terraform changes grows when we use module inputs (variables).
Specifically, how does the number of inputs affect the work Terraform does?
Analyze the time complexity of passing multiple variables into a Terraform module.
module "example" {
source = "./modules/example"
var1 = var.input1
var2 = var.input2
var3 = var.input3
# ... imagine many more variables
}
This code passes several input variables into a module to configure resources inside it.
Look at what repeats as the number of inputs grows.
- Primary operation: Terraform reads and processes each input variable to pass into the module.
- How many times: Once per input variable.
As you add more input variables, Terraform does more work reading and validating them.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 variable reads and validations |
| 100 | 100 variable reads and validations |
| 1000 | 1000 variable reads and validations |
Pattern observation: The work grows directly with the number of input variables.
Time Complexity: O(n)
This means the time to process inputs grows in a straight line as you add more variables.
[X] Wrong: "Adding more variables won't affect Terraform's processing time much."
[OK] Correct: Each variable requires reading and validation, so more variables mean more work.
Understanding how input size affects processing helps you design efficient modules and predict deployment times.
"What if we changed from many individual variables to a single map variable? How would the time complexity change?"
Practice
variables in a Terraform module?Solution
Step 1: Understand module customization
Variables allow users to provide input values to modules, changing how they behave without editing the module code itself.Step 2: Differentiate variables from other features
Variables are not for outputs or automatic resource creation; they are inputs to customize modules.Final Answer:
To customize module behavior without changing its code -> Option AQuick Check:
Module inputs = customization [OK]
- Confusing variables with outputs
- Thinking variables create resources
- Assuming variables only store secrets
region with a default value of us-west-1?Solution
Step 1: Check variable block syntax
Terraform variables use the syntax: variable "name" { type = string default = "value" } with quotes around strings.Step 2: Validate variable "region" { type = string default = "us-west-1" }
correctly uses quotes for the variable name and default string value, and sets type properly.Final Answer:
variable "region" { type = string default = "us-west-1" } -> Option DQuick Check:
Correct variable block syntax = variable "region" { type = string default = "us-west-1" } [OK]
- Missing quotes around strings
- Wrong order of attributes
- Assigning variable directly without block
variable "instance_count" {
type = number
default = 2
}And this usage inside the module:
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}What will happen if the module is called without specifying
instance_count?Solution
Step 1: Understand default variable behavior
If a variable has a default value and is not overridden, Terraform uses the default.Step 2: Apply default to resource count
Thecountargument usesvar.instance_count, which defaults to 2, so two instances are created.Final Answer:
Two AWS instances will be created -> Option BQuick Check:
Default variable value used = 2 instances [OK]
- Assuming missing variable causes error
- Thinking default is ignored
- Confusing count with instance_type
variable "env" {
type = string
}When calling the module, you forget to provide a value for
env. What will happen?Solution
Step 1: Check variable declaration for default
The variableenvhas no default value, so it is required.Step 2: Understand Terraform behavior on missing required variables
If a required variable is not provided, Terraform throws an error and stops.Final Answer:
Terraform will throw an error about missing required variable -> Option AQuick Check:
Required variable missing = error [OK]
- Assuming empty string or null is automatic default
- Thinking Terraform guesses a value
- Confusing variable name with default
Solution
Step 1: Identify correct type syntax for list of strings
Terraform useslist(string)to specify a list where each item is a string.Step 2: Check default value presence
No default means the variable is required. variable "subnet_ids" { type = list(string) } has no default, so input must be provided.Final Answer:
variable "subnet_ids" { type = list(string) } -> Option CQuick Check:
List of strings required, no default = variable "subnet_ids" { type = list(string) } [OK]
- Using wrong type like string instead of list(string)
- Providing default empty list when input should be required
- Using invalid syntax for type or default
Ready to prove it?
Pass the test and this pattern moves unknown
