0
0
Terraformcloud~5 mins

Default values in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Default values
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the default count increases, the number of instances created grows proportionally.

Input Size (n)Approx. API Calls/Operations
1010 instance creations
100100 instance creations
10001000 instance creations

Pattern observation: The work grows directly with the number of instances specified by the default value.

Final Time Complexity

Time Complexity: O(n)

This means the time to apply grows linearly with the number of instances created from the default value.

Common Mistake

[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.

Interview Connect

Understanding how default values influence resource creation helps you predict deployment time and cost, a useful skill in real cloud projects.

Self-Check

"What if we changed the default count to a variable input at runtime? How would the time complexity change?"