0
0
Terraformcloud~30 mins

Plan and apply separation in pipelines in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Plan and apply separation in pipelines
📖 Scenario: You are working in a team managing cloud infrastructure using Terraform. To keep your deployment process safe and organized, you want to separate the planning and applying steps into two different pipeline stages. This helps catch errors before making changes live.
🎯 Goal: Build a Terraform pipeline configuration that clearly separates the terraform plan step from the terraform apply step using two distinct pipeline jobs.
📋 What You'll Learn
Create a Terraform pipeline with two jobs: plan and apply
The plan job runs terraform init and terraform plan
The apply job runs terraform apply only after plan succeeds
Use a variable to control the Terraform workspace name
💡 Why This Matters
🌍 Real World
Separating plan and apply steps in Terraform pipelines is a common practice in real-world cloud infrastructure management to prevent accidental changes and improve safety.
💼 Career
Cloud engineers and DevOps professionals use this pattern daily to maintain reliable and controlled infrastructure deployments.
Progress0 / 4 steps
1
Define Terraform workspace variable
Create a Terraform variable called workspace with default value dev in a file named variables.tf. This variable will hold the workspace name.
Terraform
Need a hint?

Use the variable block with a default value.

2
Create plan job configuration
Add a pipeline job named plan that runs terraform init and terraform plan using the workspace variable var.workspace. Use a shell script block for commands.
Terraform
Need a hint?

Define a job block with steps running terraform init and terraform plan using the variable.

3
Create apply job with dependency
Add a pipeline job named apply that runs terraform apply -auto-approve using the workspace variable var.workspace. Make sure this job depends on the plan job so it runs only after planning succeeds.
Terraform
Need a hint?

Use depends_on to link the apply job to plan.

4
Complete pipeline with workspace usage
Ensure the pipeline uses the workspace variable in both jobs and that the apply job runs only after the plan job completes successfully. The final pipeline configuration should include the variable and both jobs with correct commands and dependencies.
Terraform
Need a hint?

Review that both jobs use the workspace variable and apply depends on plan.