Resource types and names in Terraform - Time & Space Complexity
When creating resources in Terraform, the time it takes depends on how many resources you define.
We want to know how the number of resource types and names affects the total work Terraform does.
Analyze the time complexity of defining multiple resources with different types and names.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
}
resource "aws_instance" "db" {
ami = "ami-654321"
instance_type = "t2.micro"
}
resource "aws_s3_bucket" "storage" {
bucket = "my-bucket"
acl = "private"
}
This code creates three resources: two instances and one storage bucket, each with unique names.
Each resource block triggers an API call to create or manage that resource.
- Primary operation: API call to provision each resource with its unique name.
- How many times: Once per resource defined in the configuration.
As you add more resource blocks with different types and names, the number of API calls grows directly with the number of resources.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows evenly as you add more resources, one API call per resource.
Time Complexity: O(n)
This means the time to apply your Terraform configuration grows directly with the number of resources you define.
[X] Wrong: "Adding more resource names of the same type does not increase the time because they share the type."
[OK] Correct: Each resource name creates a separate resource, so each one requires its own API call and provisioning time.
Understanding how resource count affects deployment time helps you plan infrastructure changes and explain your design choices clearly.
"What if we used modules to create multiple resources instead of defining them individually? How would the time complexity change?"