Terraform in GitLab CI - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
Solution
Step 1: Understand Terraform's role
Terraform is a tool designed to automate cloud infrastructure setup and changes.Step 2: Understand GitLab CI's role
GitLab CI automates running tasks like Terraform commands in a pipeline.Final Answer:
To automate the creation and management of cloud resources -> Option BQuick Check:
Terraform automates cloud resource management = B [OK]
- Confusing Terraform with application code tools
- Thinking GitLab CI monitors servers directly
- Mixing user access management with infrastructure automation
Solution
Step 1: Identify Terraform stages in GitLab CI
Common stages are validate, plan, and apply.Step 2: Match stage to syntax check
The validate stage checks Terraform files for syntax errors before any changes.Final Answer:
validate -> Option CQuick Check:
Syntax check stage = validate [OK]
- Confusing apply with validation
- Using deploy which is not a Terraform stage
- Thinking build is related to Terraform syntax
stages:
- validate
- plan
- apply
validate:
script:
- terraform validate
plan:
script:
- terraform plan -out=tfplan
apply:
script:
- terraform apply tfplan
when: manualWhat happens when the pipeline reaches the
apply stage?Solution
Step 1: Understand the 'when: manual' keyword
This setting means the apply job waits for a user to start it manually.Step 2: Check apply stage behavior
Apply will not run automatically; it requires manual approval to proceed.Final Answer:
Terraform waits for manual approval before applying changes -> Option AQuick Check:
Manual apply means wait for approval = D [OK]
- Assuming apply runs automatically
- Thinking manual means skip permanently
- Confusing plan rerun with apply stage
apply:
script:
- terraform apply tfplan
when: manual
only:
- mainBut the apply job runs on every branch, not just
main. What is the likely cause?Solution
Step 1: Recognize GitLab CI syntax changes
GitLab deprecated 'only' in favor of 'rules' for better control.Step 2: Understand effect on job filtering
Using 'only' may not filter branches correctly, causing job to run everywhere.Final Answer:
The 'only' keyword is deprecated and ignored; use 'rules' instead -> Option AQuick Check:
Use 'rules' not 'only' for branch filters [OK]
- Thinking 'when: manual' affects branch filtering
- Believing job names control execution
- Restarting pipeline without fixing config
Solution
Step 1: Configure plan job for merge requests only
Using rules with '$CI_MERGE_REQUEST_ID' ensures plan runs only on MRs.Step 2: Configure apply job for manual approval on main branch
Rules with branch check and 'when: manual' ensure manual apply on main only.Step 3: Confirm why other configurations fail
Other configurations either use the deprecated 'only' keyword or reverse the conditions (plan on main and apply on merge requests).Final Answer:
The configuration using rules with $CI_MERGE_REQUEST_ID for plan and $CI_COMMIT_BRANCH == "main" for manual apply -> Option DQuick Check:
Use 'rules' with MR and branch checks for plan/apply [OK]
- Using deprecated 'only' keyword
- Mixing up branch and merge request conditions
- Forgetting 'when: manual' for apply
