0
0
Terraformcloud~5 mins

Resource dependencies (implicit) in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Resource dependencies (implicit)
O(n)
Understanding Time Complexity

When Terraform creates resources, some depend on others. Understanding how these dependencies affect the time it takes to build infrastructure is important.

We want to know how the number of resources and their connections change the total work Terraform does.

Scenario Under Consideration

Analyze the time complexity of creating resources with implicit dependencies.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

resource "aws_instance" "web" {
  subnet_id = aws_subnet.subnet.id
  ami       = "ami-123456"
  instance_type = "t2.micro"
}

This sequence creates a VPC, then a subnet inside it, then an instance inside the subnet. Each resource depends on the previous one implicitly.

Identify Repeating Operations

Look at what Terraform does repeatedly when creating these resources.

  • Primary operation: API calls to create each resource (VPC, subnet, instance).
  • How many times: Once per resource, but each depends on the previous one finishing first.
How Execution Grows With Input

As you add more resources with dependencies, Terraform must wait for each to finish before starting the next.

Input Size (n)Approx. API Calls/Operations
1010 calls, done one after another
100100 calls, each waiting for the previous
10001000 calls, sequentially dependent

Pattern observation: The total time grows roughly in direct proportion to the number of resources because each waits for the last.

Final Time Complexity

Time Complexity: O(n)

This means the time to create all resources grows linearly with the number of dependent resources.

Common Mistake

[X] Wrong: "Terraform creates all resources at the same time, so adding more resources doesn't increase time much."

[OK] Correct: When resources depend on each other, Terraform waits for one to finish before starting the next, so time adds up.

Interview Connect

Understanding how dependencies affect deployment time helps you design infrastructure that builds efficiently and predict how long changes will take.

Self-Check

"What if some resources had no dependencies and could be created at the same time? How would that change the time complexity?"