0
0
Terraformcloud~10 mins

Why state operations are needed in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why state operations are needed
Write Infrastructure Code
Terraform Init & Plan
Check Current State File
Compare Desired vs Current State
Apply Changes to Cloud
Update State File
Ready for Next Run
Terraform uses a state file to track real infrastructure. State operations keep this file updated to know what exists and what needs change.
Execution Sample
Terraform
terraform init
terraform plan
terraform apply
Initialize Terraform, see planned changes by comparing state, then apply changes and update state.
Process Table
StepActionState File ContentCloud InfrastructureResult
1terraform initEmpty or existing state loadedNo resources yetReady to plan
2terraform planReads state fileReads current cloud resourcesShows diff between desired and actual
3terraform applyUpdates state after changesCreates/updates/deletes resourcesState matches cloud
4terraform plan (next run)Uses updated stateCloud matches stateNo changes needed
5terraform apply (next run)No state changeNo resource changeNo action taken
💡 No changes detected, state and cloud are in sync, so execution stops.
Status Tracker
VariableStartAfter InitAfter PlanAfter ApplyAfter Next PlanAfter Next Apply
state_fileempty or oldloadedread and comparedupdated with new infraread and unchangedunchanged
cloud_resourcesnonenoneexisting resources readresources created/updatedresources unchangedunchanged
Key Moments - 3 Insights
Why does Terraform need a state file?
Terraform uses the state file to remember what resources it created before, so it can compare and know what to change next (see execution_table steps 2 and 3).
What happens if the state file is out of sync with the cloud?
Terraform will detect differences and try to fix them during apply, but this can cause unexpected changes or errors (refer to execution_table step 3).
Why does Terraform do nothing if no changes are needed?
Because the state file matches the cloud resources exactly, so no action is required (see execution_table steps 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Terraform do at step 2?
ADeletes the state file
BReads the state file and compares with cloud resources
CUpdates the cloud resources directly
DCreates new resources without checking
💡 Hint
Check the 'Action' and 'Result' columns at step 2 in the execution_table.
At which step does Terraform update the state file with new infrastructure info?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look for 'Updates state after changes' in the 'State File Content' column.
If the state file was missing, what would happen at step 2?
ATerraform would think no resources exist and plan to create all
BTerraform would skip the plan step
CTerraform would delete all cloud resources
DTerraform would update the state file automatically
💡 Hint
Refer to the role of the state file in tracking existing resources in the variable_tracker.
Concept Snapshot
Terraform uses a state file to track real infrastructure.
State operations compare desired config with actual resources.
Terraform plans changes based on state differences.
Apply updates cloud and state file together.
Keeping state accurate avoids unexpected changes.
Full Transcript
Terraform manages cloud infrastructure by keeping a state file that records what resources exist. When you run terraform plan, it reads this state and compares it to the actual cloud resources to find differences. Then terraform apply makes the necessary changes and updates the state file to match. This cycle ensures Terraform knows what is deployed and what needs updating, preventing mistakes and keeping infrastructure consistent.