0
0
Terraformcloud~5 mins

Plan output reading in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Plan output reading
O(n)
Understanding Time Complexity

When Terraform creates a plan, it shows what changes it will make. Understanding how long it takes to read this plan helps us know how it scales as the plan grows.

We want to know: how does the time to read the plan grow as the number of resources increases?

Scenario Under Consideration

Analyze the time complexity of reading Terraform plan output for multiple resources.


resource "aws_instance" "example" {
  count         = var.instance_count
  ami           = var.ami_id
  instance_type = "t2.micro"
}

output "instance_ids" {
  value = aws_instance.example[*].id
}
    

This code creates multiple instances and outputs their IDs in a list.

Identify Repeating Operations

When reading the plan output, Terraform processes each resource's state and attributes.

  • Primary operation: Reading each resource's ID from the plan output.
  • How many times: Once per resource instance (equal to count).
How Execution Grows With Input

As the number of instances grows, Terraform reads more IDs to build the output list.

Input Size (n)Approx. API Calls/Operations
1010 reads of instance IDs
100100 reads of instance IDs
10001000 reads of instance IDs

Pattern observation: The number of reads grows directly with the number of instances.

Final Time Complexity

Time Complexity: O(n)

This means reading the plan output takes time proportional to the number of resources.

Common Mistake

[X] Wrong: "Reading the plan output is always fast and constant time regardless of resource count."

[OK] Correct: The plan output includes details for each resource, so reading grows with how many resources there are.

Interview Connect

Knowing how plan output reading scales helps you understand Terraform's behavior on bigger projects. This skill shows you can think about tool performance as infrastructure grows.

Self-Check

"What if we changed the output to include nested attributes for each instance? How would the time complexity change?"