Local State vs Remote State in Terraform: Key Differences and Usage
local state stores the infrastructure state file on your local machine, while remote state stores it in a shared backend like AWS S3 or Terraform Cloud. Remote state enables team collaboration and state locking, whereas local state is simpler but suited only for single-user setups.Quick Comparison
Here is a quick side-by-side comparison of local state and remote state in Terraform.
| Factor | Local State | Remote State |
|---|---|---|
| Storage Location | On local disk (e.g., terraform.tfstate file) | Cloud storage or remote backend (e.g., S3, Terraform Cloud) |
| Collaboration | Not suitable for teams; single user only | Supports multiple users with state locking |
| State Locking | No locking; risk of conflicts | Supports locking to prevent concurrent changes |
| Security | Depends on local machine security | Centralized with access controls and encryption |
| Backup & Recovery | Manual backup needed | Often automatic backups and versioning |
| Setup Complexity | Simple, no extra config | Requires backend configuration |
Key Differences
Local state means Terraform saves the state file on your own computer. This is easy to start with and requires no extra setup. However, it is risky if multiple people work on the same infrastructure because there is no way to prevent two users from changing the state at the same time.
Remote state stores the state file in a shared place like AWS S3, Azure Blob Storage, or Terraform Cloud. This allows teams to work together safely because remote backends support locking. Locking stops two users from applying changes simultaneously, avoiding conflicts and corruption.
Remote state also improves security by centralizing access controls and often encrypting the state file. It usually includes automatic backups and versioning, making recovery easier if something goes wrong. Local state relies on you to back up the file manually.
Code Comparison
This example shows how to use local state in Terraform with no backend configured.
terraform {
required_version = ">= 1.0"
}
resource "aws_s3_bucket" "example" {
bucket = "my-local-state-bucket"
acl = "private"
}Remote State Equivalent
This example configures remote state using AWS S3 as the backend with state locking via DynamoDB.
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "project/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-lock-table"
encrypt = true
}
}
resource "aws_s3_bucket" "example" {
bucket = "my-remote-state-bucket"
acl = "private"
}When to Use Which
Choose local state when you are learning Terraform, working alone, or managing small, simple projects where collaboration is not needed. It is quick to set up and requires no extra services.
Choose remote state when working in a team, managing production infrastructure, or needing reliable state locking and backups. Remote state ensures safe collaboration, better security, and easier recovery from errors.