0
0
Terraformcloud~10 mins

State and real infrastructure mapping in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - State and real infrastructure mapping
Terraform Config Files
terraform apply
Terraform State File
Compare State vs Real Infrastructure
No Action
Update State File
Terraform reads config files, applies changes, updates state file, and compares state with real infrastructure to keep them in sync.
Execution Sample
Terraform
terraform apply
# Terraform reads config
# Checks state file
# Compares with real infra
# Creates/updates resources
# Updates state file
This simulates Terraform applying infrastructure changes and updating its state file to match real resources.
Process Table
StepActionState File ContentReal InfrastructureResult
1Read config filesState file with previous resourcesExisting cloud resourcesPrepare plan
2Compare state with real infraState shows resource A existsResource A existsMatch - no change
3Compare state with real infraState shows resource B existsResource B missingDiff - resource B to create
4Create missing resource BState updated to include resource BResource B created in cloudState and infra synced
5Finish applyState file fully updatedReal infra matches stateApply complete
💡 All resources in config are created or updated; state file matches real infrastructure.
Status Tracker
VariableStartAfter Step 2After Step 4Final
State File{"resources": ["A", "B"]}{"resources": ["A", "B"]}{"resources": ["A", "B"]}{"resources": ["A", "B"]}
Real Infrastructure{"resources": ["A"]}{"resources": ["A"]}{"resources": ["A", "B"]}{"resources": ["A", "B"]}
Key Moments - 2 Insights
Why does Terraform create resource B even though it is not in the real infrastructure?
Because the state file expects resource B to exist (see step 3 in execution_table), Terraform detects the difference and creates it to match the state.
What happens if the real infrastructure has a resource not in the state file?
Terraform will detect this as a difference and may try to remove or import it depending on configuration, ensuring state and real infra match.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Terraform detect a missing resource in real infrastructure?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Result' column in execution_table rows for where 'Diff - resource B to create' appears.
According to variable_tracker, what resources exist in real infrastructure after step 4?
A["A"]
B["A", "B"]
C["B"]
D[]
💡 Hint
Look at the 'Real Infrastructure' row under 'After Step 4' in variable_tracker.
If resource B was already present in real infrastructure at start, how would step 3 change?
ATerraform would delete resource B
BIt would still show 'Diff - resource B to create'
CIt would show 'Match - no change'
DTerraform would error out
💡 Hint
Refer to step 3 in execution_table where comparison happens between state and real infra.
Concept Snapshot
Terraform uses a state file to track real infrastructure.
When you run 'terraform apply', it compares the state file with actual resources.
If differences exist, Terraform creates, updates, or deletes resources to match the state.
After changes, Terraform updates the state file to reflect reality.
This keeps your infrastructure and state in sync.
Full Transcript
Terraform manages infrastructure by keeping a state file that records what resources exist. When you run terraform apply, it reads your configuration files and compares the state file with the actual cloud resources. If it finds differences, such as missing resources, it creates or updates them. After making changes, Terraform updates the state file to match the real infrastructure. This process ensures your infrastructure matches your configuration and state file.