TerraformPilot

Troubleshooting

Fix Terraform DynamoDB Table - ResourceInUseException

Fix DynamoDB ResourceInUseException in Terraform. Handle table name conflicts, import existing tables, and manage GSI and throughput changes.

LLuca Berton1 min read

Quick Answer

#

A DynamoDB table with the same name already exists in the region. Import it into Terraform with terraform import, use a unique name, or delete the existing table if it's orphaned.

The Error

#
Error: creating DynamoDB Table (terraform-locks):
  ResourceInUseException: Table already exists: terraform-locks
Error: creating DynamoDB Table: ResourceInUseException:
  Attempt to change a resource which is still in use

What Causes This Error

#

1. Table Already Exists

#

A table with the same name was created manually, by another Terraform workspace, or by CloudFormation.

2. Table Being Modified

#

The table is currently being updated (adding/removing GSIs, changing capacity) and can't accept another modification.

3. Previous Apply Failed Mid-Creation

#

Terraform created the table but crashed before recording it in state.

How to Fix It

#

Solution 1: Import the Existing Table

#
# Check if table exists
aws dynamodb describe-table --table-name terraform-locks --region us-east-1
 
# Import into Terraform
terraform import aws_dynamodb_table.terraform_locks terraform-locks
# Ensure your config matches the existing table
resource "aws_dynamodb_table" "terraform_locks" {
  name         = "terraform-locks"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"
 
  attribute {
    name = "LockID"
    type = "S"
  }
}

Solution 2: Use Unique Names Per Environment

#
resource "aws_dynamodb_table" "app_data" {
  name         = "${var.project}-${var.environment}-data"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "pk"
  range_key    = "sk"
 
  attribute {
    name = "pk"
    type = "S"
  }
  attribute {
    name = "sk"
    type = "S"
  }
}

Solution 3: Wait for In-Progress Operations

#
# Check table status
aws dynamodb describe-table --table-name my-table \
  --query 'Table.TableStatus'
 
# Wait until ACTIVE before retrying
aws dynamodb wait table-exists --table-name my-table
terraform apply

Troubleshooting Checklist

#
  1. ✅ Does the table already exist? (aws dynamodb describe-table)
  2. ✅ Is the table currently being modified? (Status != ACTIVE)
  3. ✅ Should you import or delete the existing table?
  4. ✅ Are you using unique names per environment?

Prevention Tips

#
  • Use environment-prefixed table names to avoid collisions
  • Import existing tables instead of trying to recreate them
  • Tag tables with ManagedBy = "terraform" for easy identification
  • Use PAY_PER_REQUEST billing to avoid throughput modification conflicts
#

Conclusion

#

ResourceInUseException means a DynamoDB table with that name already exists or is being modified. Import the existing table, use unique naming conventions, or wait for in-progress operations to complete before retrying.

#Terraform#AWS#Troubleshooting#Error Fix

Share this article