Terraform init for initialization - Time & Space Complexity
We want to understand how the time to run terraform init changes as we add more modules or providers.
Specifically, how does the work grow when the project size grows?
Analyze the time complexity of running terraform init on a configuration with multiple providers and modules.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
}
}
module "network" {
source = "./modules/network"
}
module "compute" {
source = "./modules/compute"
}
This setup initializes two providers and two modules.
When running terraform init, these operations repeat:
- Primary operation: Downloading and installing each provider plugin.
- Secondary operation: Downloading module source code for each module.
- How many times: Once per provider and once per module.
As you add more providers and modules, terraform init does more work to download and prepare them.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 (providers + modules) | About 10 downloads/installations |
| 100 (providers + modules) | About 100 downloads/installations |
| 1000 (providers + modules) | About 1000 downloads/installations |
Pattern observation: The work grows roughly in direct proportion to the number of providers and modules.
Time Complexity: O(n)
This means the time to initialize grows linearly as you add more providers and modules.
[X] Wrong: "Terraform init time stays the same no matter how many modules or providers I have."
[OK] Correct: Each provider and module requires separate downloads and setup, so more items mean more work.
Understanding how initialization time grows helps you plan and manage infrastructure projects efficiently.
"What if Terraform cached all providers and modules perfectly? How would the time complexity change?"