0
0
TerraformComparisonBeginner · 4 min read

Local State vs Remote State in Terraform: Key Differences and Usage

In Terraform, 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.

FactorLocal StateRemote State
Storage LocationOn local disk (e.g., terraform.tfstate file)Cloud storage or remote backend (e.g., S3, Terraform Cloud)
CollaborationNot suitable for teams; single user onlySupports multiple users with state locking
State LockingNo locking; risk of conflictsSupports locking to prevent concurrent changes
SecurityDepends on local machine securityCentralized with access controls and encryption
Backup & RecoveryManual backup neededOften automatic backups and versioning
Setup ComplexitySimple, no extra configRequires 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
terraform {
  required_version = ">= 1.0"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-local-state-bucket"
  acl    = "private"
}
Output
Creates an S3 bucket and stores state locally in terraform.tfstate file on your machine.
↔️

Remote State Equivalent

This example configures remote state using AWS S3 as the backend with state locking via DynamoDB.

terraform
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"
}
Output
Creates an S3 bucket and stores state remotely in the specified S3 bucket with locking enabled via DynamoDB.
🎯

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.

Key Takeaways

Use local state for simple, single-user projects with minimal setup.
Use remote state to enable team collaboration and prevent state conflicts.
Remote state provides locking, security, and automatic backups.
Local state stores state on your machine; remote state stores it in shared backends.
Choose remote state for production and multi-user environments.