terraform taint and terraform apply -replace: Force Resource Recreation
Learn terraform taint, terraform untaint, and the modern terraform apply -replace. When to force-recreate resources, with examples for AWS EC2, modules
Cloud Computing
Fix terraform init S3 backend region mismatch errors. Match the region in your backend config to the actual S3 bucket location. Covers BucketRegionError
# The region in your backend config must match where the S3 bucket actually is
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-west-1" # ← Must match actual bucket region
}
}Error: Failed to get existing workspaces: S3 operation error:
BucketRegionError: incorrect region, the bucket is not in 'us-east-1' region
Error: Error configuring the backend "s3": requesting bucket region:
head bucket error: https response error StatusCode: 301Or:
Error: error using credentials to get account ID: error calling sts:GetCallerIdentity:
signed for us-east-1, bucket is in eu-west-1The region in your S3 backend config doesn't match the actual region where the S3 bucket was created.
# Your config says us-east-1
backend "s3" {
bucket = "my-terraform-state"
region = "us-east-1" # ← Config says us-east-1
}
# But the bucket is actually in eu-west-1 → ERROR# Find where the bucket actually is
aws s3api get-bucket-location --bucket my-terraform-state{
"LocationConstraint": "eu-west-1"
}Note: null or empty means us-east-1 (the default region returns null).
Then update your backend config:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-west-1" # ← Match the actual location
}
}terraform init -reconfigureIf you use -backend-config:
# Ensure region matches
terraform init \
-backend-config="bucket=my-terraform-state" \
-backend-config="key=prod/terraform.tfstate" \
-backend-config="region=eu-west-1"Or in a file:
# backend-prod.hcl
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-west-1"
dynamodb_table = "terraform-locks"terraform init -backend-config=backend-prod.hclIf you haven't stored state yet, create the bucket in your desired region:
# Create bucket in us-east-1
aws s3api create-bucket \
--bucket my-terraform-state \
--region us-east-1
# Create bucket in other regions (needs LocationConstraint)
aws s3api create-bucket \
--bucket my-terraform-state \
--region eu-west-1 \
--create-bucket-configuration LocationConstraint=eu-west-1Then match the region in config:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-west-1"
}
}These are different:
# Where STATE is stored
terraform {
backend "s3" {
bucket = "my-terraform-state"
region = "us-east-1" # ← State bucket region
}
}
# Where RESOURCES are created
provider "aws" {
region = "eu-west-1" # ← Resource region
}The backend region is where your state file lives. The provider region is where your infrastructure is created. They don't have to match.
# ❌ Using provider region for backend (they can differ)
provider "aws" { region = "eu-west-1" }
backend "s3" { region = "eu-west-1" } # Only correct if bucket is in eu-west-1
# ❌ Missing region entirely
backend "s3" {
bucket = "my-state"
key = "terraform.tfstate"
# No region! Defaults to us-east-1
}
# ❌ Using AWS_DEFAULT_REGION for backend
# Backend region must be explicit — it doesn't use AWS_DEFAULT_REGION# State in one central region, resources in multiple regions
terraform {
backend "s3" {
bucket = "company-terraform-state"
key = "us-east/prod/terraform.tfstate"
region = "us-east-1" # State always in us-east-1
}
}
provider "aws" {
alias = "us_east"
region = "us-east-1"
}
provider "aws" {
alias = "eu_west"
region = "eu-west-1"
}S3 backend region mismatch means your region config doesn't match where the bucket actually is. Find the real location with aws s3api get-bucket-location, update the backend config, and run terraform init -reconfigure. The backend region (where state lives) and provider region (where resources are created) are independent.
Learn terraform taint, terraform untaint, and the modern terraform apply -replace. When to force-recreate resources, with examples for AWS EC2, modules
Configure Terraform S3 backend for remote state storage with DynamoDB state locking. Complete setup guide with IAM permissions, encryption, and versioning.
Master Terraform state management: remote backends, state locking with DynamoDB, state commands, sensitive data, and team collaboration best practices.
Discover the importance of resource attributes and dependencies in Terraform. This guide explains how to define, access, and manage these elements to.