How to fix Error: Error creating Athena Database: AlreadyExistsException in Terraform

TerraformINTERMEDIATEMEDIUMUpdated Dec 31, 2025ยท 89 views

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
  1. Verify the database exists in AWS Athena console
  2. Use terraform import to add the existing database to state
  3. Use lowercase database names in Terraform configuration
  4. Delete the existing database if you want to recreate it
terraform โ€” error
terraform import aws_athena_database.example my_database_name
Error: 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

  1. Database already exists in AWS Glue Data Catalog from a previous deployment or manual creation
  2. Case sensitivity mismatch - Athena converts database names to lowercase but Terraform expects exact case match
  3. Terraform state is out of sync with actual AWS resources
  4. Database was created outside of Terraform and not imported into Terraform state
  5. Rerunning terraform apply without destroying the previous database first

How to fix "Error: Error creating Athena Database: AlreadyExistsException"

1Verify the database exists in AWS Athena console

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.

2Use terraform import to add the existing database to state

If the database already exists but is not in your Terraform state, import it using:

bash
terraform import aws_athena_database.example my_database_name

Replace my_database_name with your actual database name. This adds the existing database to your Terraform state without trying to recreate it.

3Use lowercase database names in Terraform configuration

Update your Terraform configuration to use lowercase database names, since Athena automatically converts all database names to lowercase. For example:

hcl
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.

4Delete the existing database if you want to recreate it

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:

bash
aws athena list-table-metadata --catalog-name hive --database-name my_database_name

Then delete the database:

bash
aws glue delete-database --name my_database_name

After deletion, run terraform apply again.

5Use force_destroy to allow Terraform to clean up on destroy

Update your aws_athena_database resource to include force_destroy to ensure Terraform can destroy the database even if it contains tables:

hcl
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.

6Validate IAM permissions for the Terraform user

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