0
0
Terraformcloud~5 mins

Terraform in GitLab CI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Terraform in GitLab CI
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As you add more resources, Terraform makes more API calls to manage them.

Input Size (n)Approx. API Calls/Operations
10About 10 calls
100About 100 calls
1000About 1000 calls

Pattern observation: The number of operations grows roughly in direct proportion to the number of resources.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the Terraform apply step grows linearly with the number of resources.

Common Mistake

[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.

Interview Connect

Understanding how Terraform scales in CI pipelines helps you design efficient infrastructure and pipelines, a useful skill in cloud roles.

Self-Check

"What if we used Terraform workspaces to split resources? How would that affect the time complexity?"