How to fix Error: Error creating Athena Database: AlreadyExistsException in Terraform
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.
Quick fix
Fixes Error: Error creating Athena Database: AlreadyExistsException
terraform import aws_athena_database.example my_database_name- Verify the database exists in AWS Athena console
- Use terraform import to add the existing database to state
- Use lowercase database names in Terraform configuration
- Delete the existing database if you want to recreate it
terraform import aws_athena_database.example my_database_nameError: Error creating Athena Database: AlreadyExistsException
What this error means
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.
Typical symptoms
- Terraform apply fails with 'AlreadyExistsException' error message
- Database appears to exist in AWS Athena console but Terraform shows it as not created
- Running terraform apply multiple times produces the same error
- Error message: 'Database [name] already exists'
- Case mismatch between Terraform config and actual database name in AWS console
Common causes
- Database already exists in AWS Glue Data Catalog from a previous deployment or manual creation
- Case sensitivity mismatch - Athena converts database names to lowercase but Terraform expects exact case match
- Terraform state is out of sync with actual AWS resources
- Database was created outside of Terraform and not imported into Terraform state
- Rerunning terraform apply without destroying the previous database first
How to fix "Error: Error creating Athena Database: AlreadyExistsException"
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.
Advanced notes
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.
Frequently asked questions
- What does "Error: Error creating Athena Database: AlreadyExistsException" mean?
- 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.
- How do I fix "Error: Error creating Athena Database: AlreadyExistsException"?
- 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.
- What causes "Error: Error creating Athena Database: AlreadyExistsException"?
- Database already exists in AWS Glue Data Catalog from a previous deployment or manual creation Case sensitivity mismatch - Athena converts database names to lowercase but Terraform expects exact case match Terraform state is out of sync with actual AWS resources
- Is the "Error: Error creating Athena Database: AlreadyExistsException" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐
- ๐
- ๐
- ๐