Integration testing strategies in Terraform - Time & Space Complexity
When running integration tests with Terraform, we want to know how the time to complete tests changes as we add more resources or modules.
We ask: How does the number of API calls and operations grow when testing more components together?
Analyze the time complexity of running integration tests that create 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 a number of virtual machines based on a variable count, then outputs their IDs for testing.
Look at what happens repeatedly when the test runs.
- Primary operation: Creating each virtual machine (API call to cloud provider)
- How many times: Once per instance, equal to
var.instance_count
As you increase the number of instances, the number of API calls grows directly with it.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 create calls |
| 100 | 100 create calls |
| 1000 | 1000 create calls |
Pattern observation: Doubling the number of instances doubles the API calls and test time.
Time Complexity: O(n)
This means the test time grows in a straight line with the number of resources tested.
[X] Wrong: "Adding more resources won't affect test time much because they run in parallel."
[OK] Correct: Even if some operations run at the same time, the cloud provider and Terraform still handle each resource separately, so total API calls and time increase with more resources.
Understanding how test time grows helps you plan and explain testing strategies clearly, showing you know how infrastructure size impacts testing effort.
"What if we grouped resources into modules and tested modules instead of individual resources? How would the time complexity change?"