0
0
Terraformcloud~10 mins

Why state is essential in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why state is essential
Write Terraform Code
Run terraform apply
Terraform creates resources
Terraform saves state file
Next run reads state file
Terraform compares desired vs actual
Apply only needed changes
End
Terraform uses a state file to remember what resources it created. This helps it know what to change next time.
Execution Sample
Terraform
terraform apply
# creates resources and saves state
terraform apply
# reads state and updates only changes
Shows how Terraform uses the state file to track resources between runs.
Process Table
StepActionState File ContentTerraform Behavior
1Run terraform apply first timeEmpty or no state fileCreates all resources as per config, saves state file with resource info
2Run terraform apply second timeState file with resource infoReads state, compares with config, no changes if config unchanged
3Change config (e.g., add resource)State file with old resourcesReads state, detects new resource, creates only new resource, updates state
4Delete resource from configState file with old resourcesReads state, detects missing resource in config, destroys resource, updates state
💡 Terraform stops after syncing real resources with config using state file to track changes.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
state fileemptycontains all created resourcesunchanged (if no config change)updated with new resourceupdated after resource deletion
Key Moments - 3 Insights
Why does Terraform need a state file?
Terraform uses the state file to remember what resources it created so it can know what to add, change, or remove next time (see execution_table steps 1 and 2).
What happens if the state file is lost?
Terraform will think no resources exist and try to create everything again, causing duplicates or errors (related to execution_table step 1).
How does Terraform know what changed in the infrastructure?
It compares the current state file with the desired config to find differences and apply only needed changes (see execution_table steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the state file contain after the first terraform apply?
AIt is empty
BIt contains all created resources
CIt contains only deleted resources
DIt contains only planned changes
💡 Hint
Check execution_table row 1 under 'State File Content'
At which step does Terraform detect a new resource to create?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 under 'Terraform Behavior'
If the state file is lost, what will Terraform do on next apply?
AUpdate only changed resources
BDestroy all resources
CCreate all resources again
DDo nothing
💡 Hint
Refer to key_moments about losing the state file
Concept Snapshot
Terraform state file stores info about created resources.
It helps Terraform know what exists and what to change.
Without state, Terraform can't track resources properly.
State is updated after each apply.
Always keep state safe and shared for teams.
Full Transcript
Terraform uses a state file to keep track of the resources it creates. When you run terraform apply the first time, it creates all resources and saves their info in the state file. On the next runs, Terraform reads this state file to know what resources already exist. It compares the current state with your configuration to decide what to add, change, or remove. This way, Terraform only makes necessary changes, avoiding duplicates or errors. Losing the state file causes Terraform to lose track of resources, which can lead to recreating everything. Therefore, the state file is essential for Terraform to manage infrastructure safely and efficiently.