0
0
Terraformcloud~10 mins

State file purpose and structure in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - State file purpose and structure
Terraform Init
Read Existing State File?
NoCreate New State File
Yes
Compare Desired Config with State
Plan Changes
Apply Changes
Update State File
Save State File Locally or Remotely
Terraform uses a state file to keep track of resources it manages. It reads the state, plans changes, applies them, then updates the state file.
Execution Sample
Terraform
terraform init
terraform plan
terraform apply
Initialize Terraform, plan infrastructure changes, then apply them updating the state file.
Process Table
StepActionState File StatusResult
1terraform initNo state file foundInitialize backend and prepare working directory
2terraform planState file empty or outdatedCompare config with state, show planned changes
3terraform applyState file updatedApply changes and write new state file
4terraform plan (next run)State file currentShow no changes needed
5terraform apply (next run)State file currentNo changes applied, state unchanged
💡 No changes detected, state file matches infrastructure
Status Tracker
VariableStartAfter InitAfter PlanAfter ApplyFinal
state_filenonenone (treated as empty)read and comparedupdated with new resource infosaved and current
Key Moments - 3 Insights
Why does Terraform need a state file?
Terraform uses the state file to remember what resources it manages and their current status, so it can plan and apply only necessary changes (see execution_table steps 2 and 3).
What happens if the state file is missing or corrupted?
Terraform will treat it as if no resources exist, potentially trying to create all resources again, which can cause conflicts or duplication (see execution_table step 1).
How does Terraform update the state file?
After applying changes, Terraform writes the new resource information to the state file to keep it in sync with real infrastructure (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state file status after 'terraform plan' is run the first time?
AState file is updated with new resources
BState file is empty or outdated
CState file is missing
DState file is current and unchanged
💡 Hint
Check the 'State File Status' column at step 2 in execution_table
At which step does Terraform write changes to the state file?
Aterraform init
Bterraform plan
Cterraform apply
Dterraform destroy
💡 Hint
Look for when the state file is 'updated' in execution_table step 3
If the state file is missing before 'terraform plan', what will Terraform do?
ACreate a new empty state file
BFail and stop execution
CSkip initialization
DApply changes without state
💡 Hint
See execution_table step 1 where no state file is found
Concept Snapshot
Terraform state file:
- Tracks resources Terraform manages
- Read at 'plan' to compare desired vs actual
- Updated after 'apply' with new info
- Stored locally or remotely
- Essential for safe, incremental changes
Full Transcript
Terraform uses a state file to keep track of the infrastructure it manages. When you run 'terraform init', it prepares the working directory and backend. During 'terraform plan', Terraform reads the state file (creating an empty one if none exists) to compare the current infrastructure with the desired configuration and shows what changes it will make. When you run 'terraform apply', Terraform makes the changes and updates the state file to reflect the new infrastructure state. This process ensures Terraform knows what resources exist and prevents accidental duplication or deletion. If the state file is missing or corrupted, Terraform may try to recreate resources, causing conflicts. The state file can be stored locally or remotely for team collaboration and safety.