0
0
Terraformcloud~5 mins

State disaster recovery in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform keeps track of your infrastructure in a state file. If this file is lost or corrupted, you can lose control over your resources. State disaster recovery means safely restoring this file to continue managing your infrastructure without problems.
When your local Terraform state file is accidentally deleted or corrupted.
When you want to recover your infrastructure management after a system crash.
When you switch to a remote backend and want to migrate your existing state safely.
When you want to keep a backup of your state file to avoid losing infrastructure tracking.
When you want to restore your infrastructure state after a failed Terraform operation.
Config File - main.tf
main.tf
terraform {
  backend "s3" {
    bucket = "example-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
    encrypt = true
  }
}

resource "aws_s3_bucket" "example" {
  bucket = "example-bucket-terraform"
  acl    = "private"
}

This configuration sets up Terraform to store its state file remotely in an AWS S3 bucket. This protects the state file from local loss and enables recovery. The bucket is where the state is saved, key is the path inside the bucket, and encrypt ensures the state file is stored securely.

The resource block creates an example S3 bucket to show infrastructure managed by Terraform.

Commands
Initializes Terraform and configures the backend to use the remote S3 bucket for state storage. This step is needed to connect Terraform to the remote state location.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Successfully configured the backend "s3"! Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
Applies the Terraform configuration to create or update infrastructure. The state file is saved remotely, so it is protected from local loss.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=example-bucket-terraform] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply without asking for confirmation
Downloads the current remote state file and saves it locally as a backup. This backup can be used to restore state if needed.
Terminal
terraform state pull > backup.tfstate
Expected OutputExpected
{ "version": 4, "terraform_version": "1.5.6", "resources": [ { "type": "aws_s3_bucket", "name": "example", "instances": [ { "attributes": { "bucket": "example-bucket-terraform", "acl": "private" } } ] } ] }
Restores the Terraform state from the backup file to the remote backend. Use this command if the remote state is lost or corrupted.
Terminal
terraform state push backup.tfstate
Expected OutputExpected
Success! State was pushed to the remote backend.
Key Concept

If you lose your Terraform state file, you can recover it by backing up and restoring the remote state to keep managing your infrastructure safely.

Common Mistakes
Not backing up the state file before making changes.
If the state file is corrupted or lost, you have no way to restore your infrastructure tracking.
Regularly run 'terraform state pull' to save a backup of your state file.
Trying to manually edit the state file to fix problems.
Manual edits can corrupt the state and cause Terraform to lose track of resources.
Use 'terraform state' commands or restore from a backup instead of manual edits.
Not configuring a remote backend for state storage.
Local state files are easy to lose or corrupt, risking infrastructure management.
Configure a remote backend like S3 to store state files safely and enable recovery.
Summary
Configure Terraform to use a remote backend like S3 to protect the state file.
Use 'terraform init' to set up the backend and 'terraform apply' to create infrastructure.
Backup the state file with 'terraform state pull' and restore it with 'terraform state push' if needed.