Skip to main content
🎤 Luca Berton is speaking at Red Hat Summit & KubeCon EU 2026!Learn more →
Back to Blog

Terraform State Management Guide

Master Terraform state management. Remote backends, state locking, import commands, state moves, and disaster recovery procedures.

Luca BertonFebruary 28, 20262 min read

Terraform state is the single most important file in your infrastructure codebase. It maps your HCL configuration to real-world resources. Lose it, and Terraform doesn't know what exists.

What Is State?

The state file (terraform.tfstate) records: - Which resources Terraform manages - Resource IDs, ARNs, and attributes - Dependencies between resources - Output values

bash
# View current state
terraform state list
terraform state show aws_instance.web

Remote State Backend

Never use local state for team projects. Set up a remote backend:

S3 + DynamoDB (AWS)

hcl
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "production/terraform.tfstate"
    region         = "eu-west-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Create the backend resources first:

hcl
# backend-setup/main.tf (run once, locally)
resource "aws_s3_bucket" "state" {
  bucket = "my-terraform-state"
}

resource "aws_s3_bucket_versioning" "state" {
  bucket = aws_s3_bucket.state.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
  bucket = aws_s3_bucket.state.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "aws:kms"
    }
  }
}

resource "aws_s3_bucket_public_access_block" "state" {
  bucket                  = aws_s3_bucket.state.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_dynamodb_table" "locks" {
  name         = "terraform-locks"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"
  attribute {
    name = "LockID"
    type = "S"
  }
}

Terraform Cloud

hcl
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "production"
    }
  }
}
Related Course

Master this topic with hands-on labs

Go beyond reading — build real projects in sandboxed environments with expert video guidance.

Browse Courses →

State Locking

Prevents two people from modifying state simultaneously:

Alice: terraform apply → Acquires lock → Makes changes → Releases lock
Bob:   terraform apply → Waits for lock → "Error: state locked by Alice"

DynamoDB provides locking for S3 backends. Terraform Cloud has built-in locking.

Force unlock (emergency only):

bash
terraform force-unlock LOCK_ID

State Operations

Import Existing Resources

Bring manually-created resources under Terraform management:

hcl
# Define the resource in HCL first
resource "aws_instance" "legacy_server" {
  ami           = "ami-0c1c30571d2dae5c9"
  instance_type = "t3.micro"
}
bash
# Import by resource ID
terraform import aws_instance.legacy_server i-0abc123def456

# Verify
terraform plan  # Should show no changes

Move Resources

Rename without destroying:

bash
# Rename a resource
terraform state mv aws_instance.old_name aws_instance.new_name

# Move into a module
terraform state mv aws_instance.web module.compute.aws_instance.web

# Move between state files
terraform state mv -state-out=other.tfstate aws_instance.web aws_instance.web

Or use moved blocks (preferred in Terraform 1.1+):

hcl
moved {
  from = aws_instance.old_name
  to   = aws_instance.new_name
}

Remove from State

Stop managing a resource without destroying it:

bash
terraform state rm aws_instance.legacy
# Resource still exists in AWS, but Terraform forgets about it

State Isolation Patterns

Per-Environment State

environments/
  dev/
    main.tf
    backend.tf    → s3://state/dev/terraform.tfstate
  staging/
    main.tf
    backend.tf    → s3://state/staging/terraform.tfstate
  production/
    main.tf
    backend.tf    → s3://state/production/terraform.tfstate

Per-Component State

infrastructure/
  network/
    backend.tf    → s3://state/network/terraform.tfstate
  database/
    backend.tf    → s3://state/database/terraform.tfstate
  application/
    backend.tf    → s3://state/application/terraform.tfstate

Reference outputs across states:

hcl
data "terraform_remote_state" "network" {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "network/terraform.tfstate"
    region = "eu-west-1"
  }
}

resource "aws_instance" "app" {
  subnet_id = data.terraform_remote_state.network.outputs.private_subnet_id
}
Stay Updated

Get weekly IT automation tips

Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.

Subscribe Free →

Disaster Recovery

S3 Versioning

With versioning enabled, recover from accidental state corruption:

bash
# List state versions
aws s3api list-object-versions --bucket my-terraform-state --prefix production/terraform.tfstate

# Restore a previous version
aws s3api get-object --bucket my-terraform-state \
  --key production/terraform.tfstate \
  --version-id "VERSION_ID" \
  restored-state.tfstate

State Backup Before Risky Operations

bash
# Manual backup
terraform state pull > backup-$(date +%Y%m%d).tfstate

# Restore from backup
terraform state push backup-20260301.tfstate

Common State Problems

ProblemSolution
State locked after crashterraform force-unlock LOCK_ID
State out of syncterraform refresh (or terraform apply -refresh-only)
Resource exists but not in stateterraform import
Want to stop managing a resourceterraform state rm
Need to rename without recreatemoved block or terraform state mv
State file corruptionRestore from S3 version history

What's Next?

Our Terraform for Beginners course covers state management in depth across 15 hands-on lessons — remote backends, locking, imports, and multi-environment patterns. First lesson is free.

---

Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.

Ready to learn by doing?

Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.

Share this article
LB
Luca Berton

Docker Captain, IT automation expert, Red Hat Summit & KubeCon speaker. Building hands-on education for DevOps engineers at CopyPasteLearn.

Related Articles

Explore topics

Browse more articles on the topics covered here.