What is the Terraform workflow with init, plan, and apply?
Learn the core Terraform workflow — init, plan, and apply — how each command works, when to run it, and why the preview-before-apply loop keeps changes safe.
Expected Interview Answer
The core Terraform workflow is a three-step cycle: 'terraform init' prepares the working directory by downloading providers and modules, 'terraform plan' computes and shows the changes needed to reach the desired state, and 'terraform apply' executes those changes against real infrastructure.
You write declarative configuration describing the infrastructure you want, then run init once per directory (or whenever providers or backends change) to set up plugins and state backend. Plan compares your configuration against the recorded state and the real provider to produce an execution plan of creates, updates, and destroys. Apply carries out that plan and records the results in state, so the next plan starts from an accurate picture. The loop repeats every time you change configuration.
- Predictable, reviewable changes before anything is touched
- Separation of preparation, preview, and execution
- State keeps track of what was actually created
- Same commands work across every provider
- Safe, repeatable automation in CI/CD pipelines
AI Mentor Explanation
Preparing for a match mirrors the Terraform loop. Init is setting up the ground the day before: rolling the pitch, checking the boundary ropes, laying out the equipment so play can begin. Plan is the captain reading the conditions and announcing the batting order and field placements before a ball is bowled, so everyone sees the intent. Apply is the actual play where those decisions take effect on the field, and the scorebook records exactly what happened for the next session.
Step-by-Step Explanation
Step 1
Write configuration
Describe the desired infrastructure declaratively in .tf files using resources, variables, and providers.
Step 2
Run terraform init
Initialize the working directory: download provider plugins and modules and configure the state backend.
Step 3
Run terraform plan
Generate an execution plan showing the additions, changes, and destructions Terraform would perform to match your config.
Step 4
Review the plan
Read the diff carefully to confirm the intended changes and catch anything unexpected before touching real resources.
Step 5
Run terraform apply
Execute the plan against the provider, create or modify resources, and update the state file with the results.
What Interviewer Expects
- Correct order and purpose of init, plan, and apply
- Understanding that init prepares providers and the backend
- Awareness that plan is a preview and apply makes changes
- Knowledge that state tracks what was created
- Recognition that the workflow is a repeatable loop
Common Mistakes
- Thinking terraform plan changes infrastructure
- Running apply without reviewing the plan first
- Forgetting that init must run when providers or backends change
- Confusing state with the configuration files
- Believing init needs to run before every single apply
Best Answer (HR Friendly)
“Terraform works in three simple steps: init gets everything ready by downloading the tools it needs, plan shows you exactly what it is about to change without doing anything yet, and apply actually makes those changes. It is like previewing an order before you confirm it, so there are no surprises.”
Code Example
# 1. Prepare the working directory (providers, modules, backend)
terraform init
# 2. Preview the changes Terraform would make
terraform plan -out=tfplan
# 3. Apply the reviewed plan to create/update infrastructure
terraform apply tfplanFollow-up Questions
- When do you need to re-run terraform init?
- What is stored in the Terraform state file?
- How does terraform apply differ from terraform plan?
- What does terraform destroy do in this workflow?
- How would you run this workflow safely inside a CI/CD pipeline?
MCQ Practice
1. Which Terraform command downloads provider plugins and configures the backend?
terraform init initializes the working directory, downloading providers and modules and setting up the state backend.
2. What does terraform plan do?
Plan produces an execution plan previewing creates, updates, and destroys, but does not change any real infrastructure.
3. After a plan is reviewed, which command actually makes the changes?
terraform apply executes the plan against the provider and records the results in state.
Flash Cards
What does terraform init do? — Prepares the working directory by downloading providers and modules and configuring the state backend.
What does terraform plan do? — Computes and previews the changes needed to reach the desired state without modifying anything.
What does terraform apply do? — Executes the plan against real infrastructure and updates the state file with the results.
Is the workflow one-time? — No — it is a repeating loop run each time the configuration changes.