Overview
Before deploying infrastructure with Terraform, you need to create a remote backend to store Terraform state files. This guide walks through setting up Google Cloud Storage (GCS) buckets for secure, versioned, and collaborative Terraform state management.State Bucket
Log Bucket
Encryption
Lifecycle
Why Remote State?
Collaboration
Collaboration
State Locking
State Locking
Disaster Recovery
Disaster Recovery
Audit Trail
Audit Trail
Prerequisites
Install gcloud CLI
Authenticate
Create/Select GCP Project
- New Project
- Existing Project
Enable Required APIs
Install Terraform
Quick Setup (5 minutes)
Navigate to Backend Setup
Create Configuration File
All Available Variables
All Available Variables
Initialize Terraform
Plan & Review
google_storage_bucket.terraform_state- State storagegoogle_storage_bucket.terraform_logs- Access logsgoogle_storage_bucket_iam_member.*- IAM bindings (if SA specified)
Apply Configuration
yes when prompted.Duration: ~30-60 secondsSave Outputs
state_bucket_name- Use this in backend configurationslog_bucket_name- For audit trail accessbackend_config_hcl- Copy-paste backend block
What Gets Created
1. State Bucket
2. Log Bucket
Using the Backend
In Environment Configurations
After backend setup, configure each environment to use the state bucket:- Production
- Staging
- Development
State Isolation Strategy
- Recommended (Prefix-based)
- Alternative (Bucket-per-Environment)
- Single backend to manage
- Cost-effective (one bucket)
- Easy to backup
- Accidental cross-environment changes possible (mitigated by permissions)
Security Best Practices
IAM Permissions
- Minimal (Recommended)
- Admin (Development)
storage.objects.get(read state)storage.objects.create(write state)storage.objects.delete(cleanup old versions)
Prevent Accidental Deletion
- Terraform Variable
- Bucket Lock (GCS)
- Organization Policy
terraform destroy from deleting the bucket.Accessing State Files
View State
Recover Previous Version
List Versions
Download Specific Version
Restore Version
Cost Analysis
GCS Pricing (us-central1)
Troubleshooting
Error: Bucket name already exists
Error: Bucket name already exists
bucket_prefix in terraform.tfvars:Error: Permission denied
Error: Permission denied
Error: API not enabled
Error: API not enabled
Error 403: Storage API has not been usedSolution:State lock conflict
State lock conflict
Error acquiring the state lockCause: Previous Terraform run didn’t release lock (crash/Ctrl+C).Solution: GCS automatically releases locks after 1 minute. Wait or:Cannot destroy backend bucket
Cannot destroy backend bucket
Error: bucket is not emptySolution:Migration from Local State
Backup Local State
Add Backend Configuration
Re-initialize
yes.Verify Migration
Advanced: Service Account Setup
For CI/CD pipelines, use a dedicated service account:Create Service Account
Grant State Bucket Access
Grant Infrastructure Permissions
Use in Backend Setup
terraform apply to grant the SA permissions to the bucket.Related Documentation
Infrastructure Overview
GCP Terraform Modules
Multi-Environment
GKE Production
Next Steps
✅ Backend Setup Complete
Review Terraform Modules
Choose Environment
Deploy Infrastructure