Resource dependencies (implicit) in Terraform - Time & Space 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.
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.
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.
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 |
|---|---|
| 10 | 10 calls, done one after another |
| 100 | 100 calls, each waiting for the previous |
| 1000 | 1000 calls, sequentially dependent |
Pattern observation: The total time grows roughly in direct proportion to the number of resources because each waits for the last.
Time Complexity: O(n)
This means the time to create all resources grows linearly with the number of dependent resources.
[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.
Understanding how dependencies affect deployment time helps you design infrastructure that builds efficiently and predict how long changes will take.
"What if some resources had no dependencies and could be created at the same time? How would that change the time complexity?"