0
0
Terraformcloud~10 mins

Local state behavior in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Local state behavior
Terraform Init
First terraform apply
Local State File Created
Subsequent Runs Read Local State
Detect Changes & Plan
Apply Changes & Update State
Repeat
Terraform initializes with 'terraform init'. The first 'terraform apply' creates the local state file. Subsequent runs read and update this file locally to detect changes and plan updates.
Execution Sample
Terraform
terraform init
terraform apply
terraform apply
Initialize Terraform, apply resources creating local state, then apply again to detect no changes.
Process Table
StepActionState File Exists?State ContentResult
1terraform initNoNoneTerraform initialized (.terraform directory created), no state file created
2terraform apply (first)NoNoneResources created, state file created with resource info
3terraform apply (second)YesHas resource infoNo changes detected, nothing applied
💡 No changes detected on second apply, so execution stops.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
local_state_fileNoneNoneCreated with resource dataUnchanged, contains resource data
Key Moments - 2 Insights
Why does Terraform not recreate resources on the second apply?
Because the local state file already has the resource info from the first apply (see execution_table step 3), Terraform knows the resources exist and skips creation.
What happens if the local state file is deleted before the second apply?
Terraform will think no resources exist and try to recreate them, since it has no local record (local_state_file variable would be None).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state content after the first terraform apply?
ANone
BHas resource info
CEmpty
DCorrupted
💡 Hint
Check the 'State Content' column in row 2 of the execution_table.
At which step does the local state file get created?
AStep 3
BStep 1
CStep 2
DBefore Step 1
💡 Hint
Look at the 'State File Exists?' column in the execution_table for step 2.
If the local state file is deleted after step 2, what will happen on the next apply?
ATerraform will detect no changes and do nothing
BTerraform will recreate all resources
CTerraform will fail with an error
DTerraform will update the state file without changes
💡 Hint
Refer to key_moments explanation about missing local state file.
Concept Snapshot
Terraform local state behavior:
- terraform init initializes working directory
- First terraform apply creates local state file with resource info
- Subsequent applies read local state to detect changes
- No changes means no resource recreation
- Deleting local state causes Terraform to lose track of resources
Full Transcript
Terraform uses a local state file to keep track of resources it manages. 'terraform init' initializes the working directory but does not create the state file. The first 'terraform apply' creates resources and the local state file with their details. On the second 'terraform apply', Terraform reads the local state file, sees the resources already exist, and detects no changes, so it does nothing. If the local state file is deleted, Terraform will think no resources exist and try to recreate them on the next apply.