Local Backend
The local backend stores state on the local filesystem, locks that state using system APIs, and performs operations locally.Implementation
Location:/internal/backend/local/backend.go
Use Cases
- Single-user workflows
- Development and testing
- Quick prototyping
- Learning Terraform
Configuration
Configuration Options
path
- Type: String
- Optional: Yes
- Default:
"terraform.tfstate" - Description: Path where the state file will be stored
workspace_dir
- Type: String
- Optional: Yes
- Default:
"terraform.tfstate.d" - Description: Directory where workspace state files are stored
Default File Paths
The local backend uses these default paths:- State file:
terraform.tfstate - Backup file:
terraform.tfstate.backup - Workspace directory:
terraform.tfstate.d
/internal/backend/local/backend.go:27-32:
Workspaces
The local backend supports multiple workspaces. Each workspace maintains its own state file:path setting, while other workspaces use subdirectories in workspace_dir.
State Locking
The local backend automatically locks state during operations that could write state. The locking mechanism varies by operating system but typically uses file locks.Backup Files
Before writing new state, the local backend creates a backup of the previous state:- Default backup path:
<state_path>.backup - Disable backups: Not supported for local backend
Command-Line Overrides
Certain Terraform commands accept flags that override the backend configuration:-state=path- Override state file path (read location)-state-out=path- Override state output path (write location)-backup=path- Override backup file path
OverrideStatePath, OverrideStateOutPath, and OverrideStateBackupPath fields (lines 62-70).
Note: These flags are NOT accepted by terraform init.
Remote State with Local Operations
The local backend can wrap another backend for state storage while performing operations locally:Example: Separate State and Backup Paths
- State is stored at:
state/terraform.tfstate - Backups are stored at:
state/terraform.tfstate.backup
Limitations
- No collaboration - State is only accessible on the local machine
- No remote locking - Locking only prevents concurrent local operations
- Risk of loss - State is not backed up remotely
- No version history - Only one backup is maintained
Best Practices
- Version control - Do NOT commit state files to Git
- Backups - Regularly back up state files
- Transition to remote - Use remote backends for team environments
- Workspace organization - Use descriptive workspace names
- Path consistency - Use relative paths for portability