Skip to main content

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.
This is a one-time setup per GCP project. Once complete, all Terraform environments (dev, staging, prod) will use these buckets.

State Bucket

Stores Terraform state with versioning

Log Bucket

Audits all state bucket access

Encryption

Google-managed encryption at rest

Lifecycle

Auto-cleanup of old versions (30 days)

Why Remote State?

Problem: Local state files can’t be shared across team members.Solution: GCS backend allows multiple engineers to work on the same infrastructure.
Problem: Concurrent Terraform runs can corrupt state.Solution: GCS provides automatic state locking (no DynamoDB needed like AWS).
Problem: Losing state file means losing track of infrastructure.Solution: GCS versioning allows recovery of previous state versions.
Problem: Need to know who accessed/modified state.Solution: Access logging tracks all operations on state bucket.

Prerequisites

1

Install gcloud CLI

Install gcloud CLI →
2

Authenticate

3

Create/Select GCP Project

4

Enable Required APIs

5

Install Terraform

Install Terraform →

Quick Setup (5 minutes)

1

Navigate to Backend Setup

2

Create Configuration File

3

Initialize Terraform

Should see: “Terraform has been successfully initialized!”
4

Plan & Review

Expected resources:
  • google_storage_bucket.terraform_state - State storage
  • google_storage_bucket.terraform_logs - Access logs
  • google_storage_bucket_iam_member.* - IAM bindings (if SA specified)
5

Apply Configuration

Type yes when prompted.Duration: ~30-60 seconds
6

Save Outputs

Outputs:
  • state_bucket_name - Use this in backend configurations
  • log_bucket_name - For audit trail access
  • backend_config_hcl - Copy-paste backend block

What Gets Created

1. State Bucket

2. Log Bucket

Purpose: Audit trail for all state bucket operations (reads, writes, deletes).

Using the Backend

In Environment Configurations

After backend setup, configure each environment to use the state bucket:
terraform/environments/gcp-prod/backend.tf
Each environment uses the same bucket but different prefixes for state isolation.

State Isolation Strategy

Recommendation: Use prefix-based strategy (one bucket) for most use cases.

Security Best Practices

IAM Permissions

Prevent Accidental Deletion

terraform.tfvars
Prevents terraform destroy from deleting the bucket.

Accessing State Files

View State

Recover Previous Version

1

List Versions

Shows all versions with generation numbers.
2

Download Specific Version

3

Restore Version


Cost Analysis

GCS Pricing (us-central1)

Backend storage costs are $0.15-0.50/month for typical usage.

Troubleshooting

Cause: GCS bucket names are globally unique across all GCP.Solution: Change bucket_prefix in terraform.tfvars:
Cause: Insufficient IAM permissions.Solution: Grant required role:
Symptom: Error 403: Storage API has not been usedSolution:
Symptom: 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:
Symptom: Error: bucket is not emptySolution:

Migration from Local State

1

Backup Local State

2

Add Backend Configuration

backend.tf
3

Re-initialize

Terraform will detect local state and ask to migrate to GCS. Type yes.
4

Verify Migration


Advanced: Service Account Setup

For CI/CD pipelines, use a dedicated service account:
1

Create Service Account

2

Grant State Bucket Access

3

Grant Infrastructure Permissions

4

Use in Backend Setup

terraform.tfvars
Re-run terraform apply to grant the SA permissions to the bucket.

Infrastructure Overview

IaC architecture and module catalog

GCP Terraform Modules

All 6 production-ready modules

Multi-Environment

Dev, staging, prod configurations

GKE Production

Deploy infrastructure to production

Next Steps

1

✅ Backend Setup Complete

You now have remote state storage configured!
2

Review Terraform Modules

3

Choose Environment

4

Deploy Infrastructure