0
0
Terraformcloud~10 mins

Terraform in GitLab CI - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform in GitLab CI
Start: GitLab CI Pipeline Triggered
Checkout Code with Terraform Files
Initialize Terraform (terraform init)
Plan Infrastructure Changes (terraform plan)
Apply Changes (terraform apply -auto-approve)
Output Results (terraform output) and End Pipeline
This flow shows how GitLab CI runs Terraform commands step-by-step to manage infrastructure automatically.
Execution Sample
Terraform
stages:
  - terraform

terraform_apply:
  stage: terraform
  script:
    - terraform init
    - terraform plan
    - terraform apply -auto-approve
    - terraform output
This GitLab CI YAML runs Terraform init, plan, apply, and output commands in order to deploy infrastructure.
Process Table
StepGitLab CI Job ActionTerraform CommandResultNext Step
1Checkout codeN/ATerraform files availableterraform init
2Initialize Terraformterraform initBackend and providers configuredterraform plan
3Plan changesterraform planShows planned infrastructure changesterraform apply
4Apply changesterraform apply -auto-approveInfrastructure created/updatedOutput results
5Output resultsterraform outputOutputs displayedPipeline ends
6Pipeline endsN/AInfrastructure deployed successfullyEND
💡 Pipeline ends after successful Terraform apply and output steps.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Terraform FilesNot checked outChecked outChecked outChecked outChecked out
Terraform StateEmptyInitialized backendPlanned changes readyUpdated with applied changesUpdated
InfrastructureNoneNonePlanned changes identifiedCreated/UpdatedCreated/Updated
OutputsNoneNoneNoneGeneratedDisplayed
Key Moments - 3 Insights
Why do we run 'terraform init' before 'terraform plan'?
'terraform init' sets up backend and providers needed for Terraform to work. Without it, 'terraform plan' cannot run properly. See execution_table step 2.
What happens if 'terraform apply' is run without '-auto-approve' in GitLab CI?
GitLab CI will wait for manual approval which blocks the pipeline. Using '-auto-approve' allows automatic apply without manual input. See execution_table step 4.
Why do we checkout code first in the pipeline?
Terraform files must be available locally to run commands. Without checkout, Terraform commands fail. See execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after running 'terraform plan' at step 3?
ABackend and providers configured
BShows planned infrastructure changes
CInfrastructure created/updated
DOutputs displayed
💡 Hint
Check the 'Result' column for step 3 in execution_table.
At which step does the GitLab CI pipeline apply infrastructure changes automatically?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'terraform apply -auto-approve' in the 'Terraform Command' column.
If the 'terraform init' command fails, what will happen to the pipeline according to the flow?
APipeline continues to 'terraform plan'
BPipeline skips to output results
CPipeline stops and reports error
DPipeline applies changes anyway
💡 Hint
Refer to the logical flow where each step depends on the previous successful step.
Concept Snapshot
Terraform in GitLab CI:
- Checkout code with Terraform files
- Run 'terraform init' to setup
- Run 'terraform plan' to preview changes
- Run 'terraform apply -auto-approve' to deploy
- Output results and finish pipeline
- Each step depends on success of previous
Full Transcript
This visual execution shows how GitLab CI automates Terraform infrastructure deployment. First, the pipeline checks out the code containing Terraform files. Then it runs 'terraform init' to configure backend and providers. Next, 'terraform plan' previews infrastructure changes. If all is good, 'terraform apply -auto-approve' deploys the changes automatically without manual approval. Finally, outputs are displayed and the pipeline ends. Each step must succeed for the next to run. This ensures safe, automated infrastructure management in GitLab CI.