Terraform in GitLab CI
📖 Scenario: You are working as a cloud engineer who wants to automate infrastructure deployment using Terraform. Your team uses GitLab for version control and CI/CD pipelines. You want to create a GitLab CI pipeline that runs Terraform commands to plan and apply infrastructure changes automatically.
🎯 Goal: Build a GitLab CI configuration file .gitlab-ci.yml that initializes Terraform, runs terraform plan, and applies the plan with terraform apply in a safe and automated way.
📋 What You'll Learn
Create a GitLab CI pipeline with three stages: init, plan, and apply
Use the official Terraform Docker image for all jobs
Initialize Terraform in the init stage
Run
terraform plan in the plan stage and save the plan outputRun
terraform apply in the apply stage using the saved planUse environment variables for Terraform backend configuration
Ensure the apply stage only runs after a successful plan
💡 Why This Matters
🌍 Real World
Automating Terraform deployments in GitLab CI saves time and reduces errors by running infrastructure commands automatically on code changes.
💼 Career
Cloud engineers and DevOps professionals use GitLab CI pipelines to manage infrastructure as code with Terraform in real projects.
Progress0 / 4 steps