This error occurs when Terraform attempts to create an AWS Athena database that already exists in your AWS Glue Data Catalog. It typically happens due to case sensitivity mismatches or state synchronization issues between Terraform and AWS.
AWS Athena stores databases in the AWS Glue Data Catalog. When you run terraform apply with an aws_athena_database resource, Terraform tries to create the database via the Glue API. If a database with that name already exists, the Glue service returns an AlreadyExistsException error. This is often caused by case sensitivity issues (Athena converts database names to lowercase) or when the database was created outside of Terraform but your Terraform configuration doesn't account for it.
Log in to the AWS Management Console and navigate to Amazon Athena. Check if the database you're trying to create already exists in the Data Catalog. Note the exact name and case used by AWS. This will help you understand if case sensitivity is the issue.
If the database already exists but is not in your Terraform state, import it using:
terraform import aws_athena_database.example my_database_nameReplace my_database_name with your actual database name. This adds the existing database to your Terraform state without trying to recreate it.
Update your Terraform configuration to use lowercase database names, since Athena automatically converts all database names to lowercase. For example:
resource "aws_athena_database" "example" {
name = "my_database_name" # Use lowercase
bucket = aws_s3_bucket.example.id
}This prevents case sensitivity mismatches between Terraform and AWS.
If you want Terraform to create a fresh database, manually delete the existing one from the AWS Athena console (or via AWS CLI). First, you must delete all tables in the database:
aws athena list-table-metadata --catalog-name hive --database-name my_database_nameThen delete the database:
aws glue delete-database --name my_database_nameAfter deletion, run terraform apply again.
Update your aws_athena_database resource to include force_destroy to ensure Terraform can destroy the database even if it contains tables:
resource "aws_athena_database" "example" {
name = "my_database_name"
bucket = aws_s3_bucket.example.id
force_destroy = true
}Note: force_destroy = true will delete all tables in the database when you run terraform destroy. Tables are not recoverable.
Ensure your AWS credentials have the necessary permissions. Terraform needs:
- glue:CreateDatabase
- glue:GetDatabase
- glue:DeleteDatabase
- s3:GetBucketLocation
- s3:ListBucket
- s3:GetObject
If permissions are restricted, the error may not clearly indicate the real issue. Add these permissions to your IAM policy if missing.
Case sensitivity in Athena/Glue is a common source of confusion. The Glue API automatically lowercases database names, but Terraform may report the database as 'not found' using the exact case you specified. Always use lowercase in your Terraform configuration to avoid this issue. If you encounter persistent state sync issues, consider using 'ignore_changes' lifecycle rule on the 'bucket' argument, since AWS doesn't expose this value via API after creation, causing Terraform to always detect a change.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform