Bulk import strategies in Terraform - Time & Space Complexity
When importing many resources in Terraform, it is important to understand how the time to complete the import grows as the number of resources increases.
We want to know how the number of import operations affects the total time taken.
Analyze the time complexity of the following operation sequence.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
# Import each instance by running a loop
# terraform import aws_instance.example[INDEX] i-INSTANCEID
This sequence defines multiple instances and imports each one individually using a loop over the count.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Terraform import command for each resource, which calls the cloud provider API to read resource state.
- How many times: Once per resource, so the number of imports equals the number of resources.
Each resource requires a separate import call, so as the number of resources grows, the total import operations grow proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 import calls |
| 100 | 100 import calls |
| 1000 | 1000 import calls |
Pattern observation: The number of import operations grows linearly with the number of resources.
Time Complexity: O(n)
This means the total time to import grows directly in proportion to how many resources you import.
[X] Wrong: "Importing multiple resources at once will take the same time as importing one resource."
[OK] Correct: Each resource import is a separate API call and takes time, so importing many resources adds up the time linearly.
Understanding how bulk operations scale helps you design efficient infrastructure workflows and shows you can think about system behavior as it grows.
"What if we batch multiple resource imports into a single API call? How would the time complexity change?"