Overview
Backends in Terraform determine where and how state is loaded, stored, and managed. They provide the abstraction that allows Terraform to support both local and remote operations seamlessly.Backend Interface
The core backend interface is defined ininternal/backend/backend.go:
internal/backend/backend.go:44-106
Backend Lifecycle
1. Configuration Schema
Backends expose their configuration requirements through a schema:configschema.Block describing the expected configuration structure.
Reference: internal/backend/backend.go:47-50
2. Configuration Preparation
Before configuration is applied, it must be validated and normalized:- Validates configuration structure
- Inserts missing defaults
- Does NOT access external data (env vars, disk files)
- Must be called before
Configure()
internal/backend/backend.go:52-70
3. Backend Configuration
Once validated, the backend is configured:internal/backend/backend.go:72-84
Workspace Management
Default Workspace
Every backend must support a default workspace:internal/backend/backend.go:21-22
Multiple Workspaces
Some backends support multiple named workspaces:internal/backend/backend.go:103-105
Workspace Errors
Backends may return specific errors:internal/backend/backend.go:24-38
State Manager
TheStateMgr method returns a state manager implementing statemgr.Full:
State Manager Responsibilities
- Create on Demand: If workspace doesn’t exist, it will be created
- Locking: If the state manager implements
statemgr.Locker, caller must handle locking - Persistence: State manager handles reading and writing state
internal/backend/backend.go:86-94
Workspace Operations
Deleting Workspaces
name: Workspace name to deleteforce: Whether to force deletion of non-empty workspace
internal/backend/backend.go:96-101
Backend Types
Local Backends
Local backends store state on the local filesystem or in memory. They are used for:- Development environments
- Single-user workflows
- Testing
Remote Backends
Remote backends store state in a remote location:- Cloud storage (S3, Azure Blob, GCS)
- HashiCorp Terraform Cloud
- Consul
- etcd
- Team collaboration
- State locking
- Remote operations
- State versioning
Backend Initialization
Backends are initialized through an initialization function:internal/backend/backend.go:41
Enhanced Backends
Some backends provide enhanced functionality beyond basic state storage:Local Operations
Thebackendrun.Local interface extends Backend to support local execution:
terraform import.
Reference: internal/command/import.go:166-170
Remote Operations
Some backends can execute operations remotely:- Terraform Cloud/Enterprise
- Custom remote execution backends
Backend Configuration Example
Local Backend
S3 Backend
Terraform Cloud Backend
Backend Migration
When changing backends, Terraform can migrate state:- Update backend configuration
- Run
terraform init - Terraform detects backend change
- Prompts to migrate state
- Copies state to new backend
Always backup your state before migrating backends. The old backend state remains unchanged until you confirm the migration.
State Locking Support
Not all backends support state locking. Check backend documentation:- With Locking: S3 (with DynamoDB), Terraform Cloud, Consul, etcd
- Without Locking: Local (basic), HTTP, artifactory
Backend State Encryption
Many backends support state encryption:At-Rest Encryption
- S3: Server-side encryption (SSE)
- Azure Blob: Storage Service Encryption
- GCS: Default encryption
- Terraform Cloud: Encrypted by default
In-Transit Encryption
All remote backends should use HTTPS/TLS for data transmission.Best Practices
Use Remote Backends for Teams
Use Remote Backends for Teams
Always use remote backends with locking support for team environments to prevent concurrent modifications.
Enable State Encryption
Enable State Encryption
Enable encryption at rest and in transit for all production backends.
Version Your State
Version Your State
Use backends that support state versioning (S3 with versioning, Terraform Cloud) to enable rollback capabilities.
Backup State Regularly
Backup State Regularly
Even with versioning, maintain separate backups of your state files.
Separate Environments
Separate Environments
Use different backends or workspaces for different environments (dev, staging, production).
Document Backend Configuration
Document Backend Configuration
Keep backend configuration in version control and document access requirements.
Workspace vs Environment
Workspaces and environments serve different purposes:- Workspaces: Multiple named state files within the same backend
- Environments: Completely separate infrastructure (often different backends)
Workspaces are best for variations of the same infrastructure. Use separate backends for truly isolated environments.
Related Resources
- State Overview - Core state concepts and data structures
- State Locking - Detailed locking mechanisms
- Remote State - Using remote state data sources
- State Manipulation - State command reference
Source Code References
- Backend Interface:
internal/backend/backend.go - Local Backend:
internal/backend/local/ - Backend Run:
internal/backend/backendrun/ - State Manager:
internal/states/statemgr/