0
0
Terraformcloud~30 mins

Terraform in GitLab CI - Mini Project: Build & Apply

Choose your learning style9 modes available
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 output
Run terraform apply in the apply stage using the saved plan
Use 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
1
Create the basic GitLab CI pipeline structure
Create a file named .gitlab-ci.yml with a stages section that lists the stages init, plan, and apply in that order.
Terraform
Need a hint?

Define the stages key as a list with the three stages in order.

2
Add the init job to initialize Terraform
Add a job named terraform_init that runs in the init stage. Use the image hashicorp/terraform:latest. The job should run the command terraform init. Use environment variables TF_BACKEND_CONFIG for backend configuration by passing -backend-config=${TF_BACKEND_CONFIG} to terraform init.
Terraform
Need a hint?

Define the job with the correct name, stage, image, and script command using the environment variable.

3
Add the plan job to create a Terraform plan
Add a job named terraform_plan that runs in the plan stage. Use the same Terraform image. The job should run terraform init -backend-config=${TF_BACKEND_CONFIG} followed by terraform plan -out=tfplan to save the plan output to a file named tfplan. Make sure this job depends on the terraform_init job by using needs. Also, add artifacts to save the tfplan file for the next stage.
Terraform
Need a hint?

Use needs to depend on the init job and artifacts to pass the plan file.

4
Add the apply job to apply the Terraform plan
Add a job named terraform_apply that runs in the apply stage. Use the Terraform image. This job should depend on terraform_plan using needs. It should run terraform init -backend-config=${TF_BACKEND_CONFIG} followed by terraform apply -auto-approve tfplan to apply the saved plan without manual approval.
Terraform
Need a hint?

Use needs to depend on the plan job and run terraform apply with the saved plan file.