By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(For engineers who need to deploy, debug, or certify—fast.)
Remote state backends are where Terraform stores the current state of your infrastructure (what’s actually deployed) so your team can collaborate without stepping on each other’s changes. Think of it like a shared Google Doc for your cloud resources—if two people edit the same file at once, chaos ensues. Remote backends prevent that.
terraform.tfstate
terraform apply
You’re a cloud engineer at a startup. Your team just migrated from manual AWS console clicks to Terraform. One teammate runs terraform apply while another is mid-deploy—boom, your RDS instance gets recreated, causing 10 minutes of downtime. Remote state backends fix this.
terraform state
apply
backend.tf
soft delete
object versioning
aws configure
terraform --version
# Create S3 bucket (replace <BUCKET_NAME> with a globally unique name) aws s3api create-bucket --bucket <BUCKET_NAME> --region us-east-1 # Enable versioning (critical for recovery) aws s3api put-bucket-versioning --bucket <BUCKET_NAME> --versioning-configuration Status=Enabled # Create DynamoDB table for state locking aws dynamodb create-table \ --table-name terraform-lock \ --attribute-definitions AttributeName=LockID,AttributeType=S \ --key-schema AttributeName=LockID,KeyType=HASH \ --billing-mode PAY_PER_REQUEST \ --region us-east-1
Create backend.tf:
terraform { backend "s3" { bucket = "<BUCKET_NAME>" key = "terraform/vpc/state.tfstate" # Path to state file region = "us-east-1" dynamodb_table = "terraform-lock" # Locking table encrypt = true # Enable encryption } }
terraform init
Expected output:
Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes.
Create vpc.tf:
vpc.tf
resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" tags = { Name = "production-vpc" } }
Deploy:
Verify state is in S3:
aws s3 ls s3://<BUCKET_NAME>/terraform/vpc/ --recursive
You should see state.tfstate.
state.tfstate
hcl # Example IAM policy for Terraform { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::<BUCKET_NAME>", "arn:aws:s3:::<BUCKET_NAME>/*" ] }, { "Effect": "Allow", "Action": [ "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:DeleteItem" ], "Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/terraform-lock" } ] }
STANDARD_IA
PAY_PER_REQUEST
terraform/<env>/<component>/state.tfstate
terraform/prod/vpc/state.tfstate
Environment=prod
Team=infra
bash terraform workspace new prod terraform workspace select prod
terraform destroy
aws s3api put-bucket-versioning
backend.hcl
encrypt = true
"You need a highly available, encrypted state backend with locking. Which AWS service do you use?" Answer: S3 + DynamoDB (S3 for state, DynamoDB for locking).
State corruption recovery:
"A teammate accidentally ran terraform destroy. How do you recover?" Answer: Restore from S3 versioning (if enabled) or re-import resources.
Terraform Cloud vs. S3:
Challenge: Deploy a GCS backend for a GCP project. Configure it to: 1. Store state in a bucket named tf-state-<PROJECT_ID>.2. Enable object versioning.3. Use a service account with least privilege.
tf-state-<PROJECT_ID>
Solution:
# Create GCS bucket gsutil mb -p <PROJECT_ID> gs://tf-state-<PROJECT_ID>/ gsutil versioning set on gs://tf-state-<PROJECT_ID>/ # Create service account (replace <SA_NAME>) gcloud iam service-accounts create <SA_NAME> \ --display-name "Terraform State SA" # Grant permissions gcloud projects add-iam-policy-binding <PROJECT_ID> \ --member "serviceAccount:<SA_NAME>@<PROJECT_ID>.iam.gserviceaccount.com" \ --role "roles/storage.admin" # Configure backend (backend.tf) terraform { backend "gcs" { bucket = "tf-state-<PROJECT_ID>" prefix = "terraform/state" credentials = "terraform-sa.json" # Service account key } }
Why it works: GCS provides versioning and encryption by default, and the service account has only the permissions it needs.
terraform init -backend-config=...
terraform state list
terraform state rm <RESOURCE>
aws s3api get-bucket-versioning
terraform workspace list
bucket
key
region
dynamodb_table
encrypt
storage_account_name
container_name
resource_group_name
prefix
credentials
hostname
organization
workspaces
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.