Cloud ComputingTerraform Remote Backends with AWS S3
Configure Terraform S3 backend for remote state storage with DynamoDB state locking. Complete setup guide with IAM permissions, encryption, and versioning.
Cloud Computing
Master Terraform state management: remote backends, state locking with DynamoDB, state commands, sensitive data, and team collaboration best practices.

Terraform state tracks every resource you manage. Store it remotely (S3, Azure Blob, GCS) with locking (DynamoDB, native) to enable team collaboration and prevent corruption. Never edit state files manually — use terraform state commands.
The state file (terraform.tfstate) is a JSON file that maps your HCL configuration to real cloud resources:
{
"resources": [{
"type": "aws_instance",
"name": "web",
"instances": [{
"attributes": {
"id": "i-1234567890abcdef0",
"ami": "ami-0abcdef1234567890",
"instance_type": "t3.micro",
"public_ip": "54.123.45.67"
}
}]
}]
}Without state, Terraform can't know which cloud resources it created, what to update, or what to destroy.
| Feature | Local (terraform.tfstate) | Remote (S3/Azure/GCS) |
|---|---|---|
| Team access | ❌ Single machine | ✅ Shared |
| Locking | ❌ None | ✅ DynamoDB/native |
| Encryption | ❌ Plaintext | ✅ At rest |
| Versioning | ❌ No history | ✅ Rollback |
| CI/CD | ❌ Needs file sharing | ✅ Native |
| Risk of loss | ❌ Laptop crash = gone | ✅ Cloud durability |
terraform {
backend "s3" {
bucket = "mycompany-terraform-state"
key = "prod/app/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}terraform {
backend "azurerm" {
resource_group_name = "terraform-rg"
storage_account_name = "tfstatemycompany"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}terraform {
backend "gcs" {
bucket = "mycompany-terraform-state"
prefix = "prod/app"
}
}terraform apply starts → requests a lockapply → sees "Error acquiring state lock" → waits# If a lock gets stuck (crashed process, lost connection):
terraform force-unlock LOCK_ID
# Get LOCK_ID from the error message# List all resources in state
terraform state list
# Show details of one resource
terraform state show aws_instance.web
# Move a resource (rename without destroy/recreate)
terraform state mv aws_instance.web aws_instance.app
# Remove from state (resource stays in cloud, Terraform forgets it)
terraform state rm aws_instance.web
# Import existing resource into state
terraform import aws_instance.web i-1234567890abcdef0
# Pull remote state to local file (backup)
terraform state pull > backup.tfstate
# Push local state to remote (dangerous — use carefully)
terraform state push backup.tfstate
# Refresh state from cloud (sync drift)
terraform apply -refresh-onlyState files contain everything — including passwords, API keys, and connection strings:
# This is in your state file in plaintext:
# "password": "super-secret-db-password"Protect state with:
*.tfstate to .gitignoreSplit state by environment and component to reduce blast radius:
# One giant state = high risk
prod/everything/terraform.tfstate ← one bad apply affects everything
# Split state = low risk
prod/networking/terraform.tfstate ← VPC, subnets, security groups
prod/database/terraform.tfstate ← RDS, ElastiCache
prod/compute/terraform.tfstate ← EC2, ASG, ALB
prod/dns/terraform.tfstate ← Route53| Problem | Solution |
|---|---|
| "Error acquiring state lock" | Wait for other apply, or force-unlock |
| State file corrupted | Restore from S3 versioning |
| Drift (cloud ≠ state) | terraform apply -refresh-only |
| Resource in wrong state file | terraform state mv -state-out=other.tfstate |
| Need to see what's in state | terraform state list then terraform state show |
terraform state commandsprevent_destroy on state storage resourcesterraform plan before apply — always review changesTerraform state is the single source of truth for your infrastructure. Store it remotely with locking and encryption, never edit it manually, split it by component, and treat it like a database — because it is one. Master terraform state commands and you'll handle any state issue that comes up.
Cloud ComputingConfigure Terraform S3 backend for remote state storage with DynamoDB state locking. Complete setup guide with IAM permissions, encryption, and versioning.
Cloud ComputingLearn terraform taint, terraform untaint, and the modern terraform apply -replace. When to force-recreate resources, with examples for AWS EC2, modules
Cloud ComputingDiscover the importance of resource attributes and dependencies in Terraform. This guide explains how to define, access, and manage these elements to.
Cloud ComputingFix terraform init S3 backend region mismatch errors. Match the region in your backend config to the actual S3 bucket location. Covers BucketRegionError