Terraform Backend Types Compared - S3, GCS, Azure, Terraform Cloud
Complete comparison of Terraform backend types: S3, GCS, Azure Blob, Terraform Cloud, and more. Step-by-step guide with code examples and best practices for ...
Guides
Complete guide to Terraform state: what it is, why it matters, remote backends, state locking, essential commands, and best practices for teams.
Terraform state is a JSON file that maps your HCL code to real cloud resources. Without it, Terraform can't know what exists. Store it remotely (S3, Azure Blob, GCS) with locking for team safety. Never edit it manually.
Every time you terraform apply, Terraform records what it created:
{
"resources": [{
"type": "aws_instance",
"name": "web",
"instances": [{
"attributes": {
"id": "i-0abc123def456",
"ami": "ami-0abcdef1234",
"instance_type": "t3.micro"
}
}]
}]
}This file (terraform.tfstate) is how Terraform knows:
| Without State | With State |
|---|---|
| Terraform creates duplicates every apply | Updates existing resources |
| Can't track dependencies | Knows resource relationships |
| Can't detect drift | Compares state vs cloud reality |
| Can't destroy resources | Knows what to delete |
# Local state (default — single user only)
ls terraform.tfstate
# Remote state (teams, CI/CD)
terraform {
backend "s3" {
bucket = "my-state-bucket"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}# List everything in state
terraform state list
# Show one resource's details
terraform state show aws_instance.web
# Move/rename a resource
terraform state mv aws_instance.web aws_instance.app
# Remove from state (keeps cloud resource)
terraform state rm aws_instance.web
# Import existing cloud resource
terraform import aws_instance.web i-0abc123
# Sync state with cloud reality
terraform apply -refresh-only
# Download state to local file (backup)
terraform state pull > backup.tfstateLocking prevents two people from modifying state simultaneously:
# User A runs terraform apply → acquires lock
# User B runs terraform apply → "Error acquiring state lock"
# User A finishes → lock released → User B can proceedBackend locking support:
⚠️ State contains sensitive data in plaintext — passwords, API keys, connection strings.
Protect it:
*.tfstate to git| Problem | Solution |
|---|---|
| "Error acquiring state lock" | Wait or terraform force-unlock LOCK_ID |
| Drift (cloud ≠ state) | terraform apply -refresh-only |
| Resource in wrong state | terraform state mv |
| State corrupted | Restore from S3 versioning |
| Forgot to import | terraform import resource.name cloud_id |
State is the heart of Terraform — it's how Terraform tracks reality. Store it remotely, lock it, encrypt it, version it, and never touch the JSON directly. Master terraform state commands and you'll handle any state issue that comes up.
Complete comparison of Terraform backend types: S3, GCS, Azure Blob, Terraform Cloud, and more. Step-by-step guide with code examples and best practices for ...
How to use Terraform workspaces to manage dev, staging, and production environments with one configuration. Commands, patterns, and when to avoid workspaces.
Practical Terraform patterns to reduce AWS costs: right-sizing, spot instances, scheduling, and reserved capacity. Step-by-step guide with code examples and ...
How to achieve zero-downtime deployments with Terraform using blue-green, rolling updates, and create_before_destroy. Step-by-step guide with code examples a...