What if your cloud setup could update itself perfectly every time you push code?
Why Terraform in GitLab CI? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have to set up cloud servers and networks by clicking through a web console every time you want to make a change.
Now, imagine doing this for multiple projects and environments, one by one.
This manual way is slow and tiring.
It's easy to forget a step or make a typo, causing errors that are hard to find.
Also, tracking what changed and when becomes a nightmare.
Using Terraform in GitLab CI automates these steps.
Every change is written as code and tested automatically when you push it.
This means your cloud setup is consistent, repeatable, and easy to track.
Click on cloud console > Create server > Configure settings > Repeat for each changeWrite Terraform code > Push to GitLab > GitLab CI runs Terraform apply automatically
You can safely and quickly manage cloud infrastructure changes with confidence and full history.
A team updates their app's servers by changing Terraform files and pushing to GitLab, triggering automatic updates without manual clicks.
Manual cloud setup is slow, error-prone, and hard to track.
Terraform in GitLab CI automates and tests infrastructure changes.
This leads to faster, safer, and more reliable cloud management.
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
