Skip to main content
Fix Terraform Error - Error Loading State from S3

Fix Terraform Error - Error Loading State from S3

Key Takeaway

How to fix 'Error loading state' from S3 backends including AccessDenied, NoSuchBucket, and corrupted state file issues.

Table of Contents

The Error

Error refreshing state: AccessDenied / NoSuchBucket / state data in S3 does not have the expected content

What Causes This

Terraform cannot load or parse the state file from S3. Common causes: IAM permissions too restrictive, bucket doesn’t exist, bucket is in wrong region, state file was manually edited and corrupted, or KMS encryption key access issues.

How to Fix It

Solution 1: Check IAM Permissions

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::my-terraform-state",
        "arn:aws:s3:::my-terraform-state/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "dynamodb:GetItem",
        "dynamodb:PutItem",
        "dynamodb:DeleteItem"
      ],
      "Resource": "arn:aws:dynamodb:*:*:table/terraform-locks"
    }
  ]
}

Solution 2: Verify Bucket and Region

aws s3 ls s3://my-terraform-state/
aws s3api get-bucket-location --bucket my-terraform-state
terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"  # Must match bucket region!
  }
}

Solution 3: Fix Corrupted State

# Download the state file
aws s3 cp s3://my-terraform-state/prod/terraform.tfstate .

# Check if it's valid JSON
python3 -m json.tool terraform.tfstate

# If corrupted, restore from S3 versioning
aws s3api list-object-versions \
  --bucket my-terraform-state \
  --prefix prod/terraform.tfstate

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

Prevention Tips

  1. Pin provider versions — avoid surprise breaking changes
  2. Use CI/CD — catch errors before they hit production
  3. Test with terraform plan — always review before applying
  4. Keep Terraform updated — newer versions have better error messages
  5. Use terraform validate — catches syntax errors early

Hands-On Courses

Learn to avoid these errors with interactive, project-based courses:

Conclusion

This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.