Terraform in GitLab CI - Time & Space Complexity
When using Terraform in GitLab CI, it's important to understand how the time to run your infrastructure changes as you add more resources.
We want to know how the number of Terraform operations grows when the infrastructure size grows.
Analyze the time complexity of this Terraform job in GitLab CI pipeline.
terraform init
terraform plan -out=plan.out
terraform apply plan.out
This sequence initializes Terraform, creates a plan for changes, and applies those changes to the cloud.
Look at what happens repeatedly during the pipeline run.
- Primary operation: Terraform API calls to create, update, or delete resources.
- How many times: Once per resource in the plan during apply.
As you add more resources, Terraform makes more API calls to manage them.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of operations grows roughly in direct proportion to the number of resources.
Time Complexity: O(n)
This means the time to run the Terraform apply step grows linearly with the number of resources.
[X] Wrong: "Terraform apply time stays the same no matter how many resources I have."
[OK] Correct: Each resource needs its own API call, so more resources mean more time.
Understanding how Terraform scales in CI pipelines helps you design efficient infrastructure and pipelines, a useful skill in cloud roles.
"What if we used Terraform workspaces to split resources? How would that affect the time complexity?"