Overview
Terraform state is a critical component that tracks the resources managed by your infrastructure as code. The state system provides a mapping between your configuration files and the real-world resources they represent.What is Terraform State?
Terraform state is a JSON file that stores information about your managed infrastructure and configuration. It serves as the source of truth for Terraform to understand what resources exist, their current attributes, and relationships between them.Key Functions
- Resource Tracking: Maps configuration to real-world resources
- Metadata Storage: Stores resource metadata and dependencies
- Performance Optimization: Caches resource attributes to avoid unnecessary API calls
- Collaboration: Enables team collaboration through shared state
State Data Structure
Terraform’s state is organized hierarchically using several core data structures defined ininternal/states/:
State Object
The top-levelState type represents the entire state file:
internal/states/state.go:27-52
Module State
Each module instance has its own state container:internal/states/module.go:11-17
Resource State
Resources track provider configuration and instances:internal/states/resource.go:13-29
Resource Instance Objects
Each resource instance contains current and deposed objects:internal/states/resource.go:59-69
Instance Object Details
The actual resource data is stored inResourceInstanceObject:
internal/states/instance_object.go:26-58
Object Status Values
Resource objects can have three status states:ObjectReady(‘R’): Object is ready to useObjectTainted(‘T’): Object is in a bad state and must be replacedObjectPlanned(‘P’): Placeholder for planned but not yet created objects
internal/states/instance_object.go:85-105
State Synchronization
For concurrent access, Terraform providesSyncState wrapper:
SyncState wrapper ensures thread-safe state operations during graph walks and other concurrent operations.
Reference: internal/states/sync.go:36-40
Key Methods
SetResourceInstanceCurrent(): Updates the current object for an instanceDeposeResourceInstanceObject(): Moves current object to deposedForgetResourceInstanceAll(): Removes all objects for an instanceLock()/Unlock(): Provides explicit locking for batch operations
internal/states/sync.go:214-482
State Mutability Rules
Concurrency Safety
Frominternal/states/state.go:20-26:
Access to State and the nested values within it is not concurrency-safe, so when accessing a State object concurrently it is the caller’s responsibility to ensure that only one write is in progress at a time and that reads only occur when no write is in progress.Solution: Use
SyncState wrapper for concurrent access patterns.
State Operations
Creating Empty State
Checking if State is Empty
internal/states/state.go:76-89
Moving Resources
The state package provides methods for moving resources between addresses:MoveAbsResource(): Move an entire resourceMoveAbsResourceInstance(): Move a single instanceMoveModuleInstance(): Move an entire module
internal/states/state.go:418-604
State Encoding
Resource instance objects are encoded toResourceInstanceObjectSrc for persistence:
- Removing sensitive marks
- Converting unknown values to null
- Marshaling to JSON
- Sorting dependencies for stable output
internal/states/instance_object.go:120-180
Best Practices
Use Accessor Methods
Use Accessor Methods
Always use the provided accessor methods rather than directly modifying state fields. This ensures invariants are maintained.
Handle Concurrency Properly
Handle Concurrency Properly
Wrap state in
SyncState for concurrent operations. Use explicit Lock()/Unlock() only when necessary.Validate Module Addresses
Validate Module Addresses
The root module cannot be removed from state. Always check
addr.IsRoot() before removal operations.Prune Empty Modules
Prune Empty Modules
State automatically prunes empty non-root modules to maintain cleanliness.
Related Resources
- State Backends - Backend configuration and remote state
- Remote State - Working with remote state data sources
- State Locking - Understanding state locking mechanisms
- State Commands - CLI commands for state manipulation
Source Code References
- Core State:
internal/states/state.go - Module State:
internal/states/module.go - Resource State:
internal/states/resource.go - Instance Objects:
internal/states/instance_object.go - Synchronization:
internal/states/sync.go - Package Documentation:
internal/states/doc.go