TerraformPilot

DevOps

Fix Terraform Error - Error Creating Glue Job - AlreadyExistsException

Fix AlreadyExistsException when creating AWS Glue jobs in Terraform. Import existing jobs, handle name conflicts, and manage Glue job state.

LLuca Berton1 min read

Quick Answer

#

A Glue job with that name already exists in AWS. Import it into Terraform state with terraform import aws_glue_job.my_job my-etl-job, or use a unique name.

The Error

#
Error: error creating Glue Job (my-etl-job): AlreadyExistsException: 
Job already exists: my-etl-job

What Causes This

#

1. Job Created Outside Terraform

#

Someone created the Glue job manually in the AWS Console or via CLI.

2. Previous Apply Partially Failed

#

Terraform created the job in AWS but failed to write the state — so it tries to create again.

3. Duplicate Job Names Across Configs

#

Two Terraform configs managing the same Glue job name.

How to Fix It

#

Solution 1: Import the Existing Job

#
# Import format: terraform import aws_glue_job.RESOURCE_NAME JOB_NAME
terraform import aws_glue_job.etl my-etl-job

Then verify:

terraform plan
# Adjust config to match the imported job's settings

Solution 2: Use Unique Job Names

#
resource "aws_glue_job" "etl" {
  name     = "${var.project}-${var.environment}-etl"
  role_arn = aws_iam_role.glue.arn
 
  command {
    name            = "glueetl"
    script_location = "s3://${aws_s3_bucket.scripts.id}/etl.py"
    python_version  = "3"
  }
 
  default_arguments = {
    "--job-language"          = "python"
    "--TempDir"               = "s3://${aws_s3_bucket.temp.id}/tmp/"
    "--enable-metrics"        = "true"
    "--enable-spark-ui"       = "true"
    "--spark-event-logs-path" = "s3://${aws_s3_bucket.logs.id}/spark-logs/"
  }
 
  max_retries       = 1
  timeout           = 60
  number_of_workers = 2
  worker_type       = "G.1X"
  glue_version      = "4.0"
}

Solution 3: Delete and Recreate

#
# If you don't need the existing job
aws glue delete-job --job-name my-etl-job
terraform apply

Solution 4: Check State for Orphans

#
terraform state list | grep glue
# If the job isn't in state but exists in AWS → import it
# If it's in state with wrong name → terraform state rm, then re-add

Troubleshooting Checklist

#
  1. ✅ Does the job exist in AWS Console? → Import it
  2. ✅ Did a previous apply fail mid-way? → Import the partially-created job
  3. ✅ Is another Terraform config managing this job? → Consolidate
  4. ✅ Is the job name unique per environment? → Add environment prefix

Prevention Tips

#
  • Always prefix job names with project + environment to avoid collisions
  • Use terraform import before managing pre-existing Glue resources
  • Check terraform state list before creating jobs that might already exist
#

Conclusion

#

AlreadyExistsException means a Glue job with that name exists in AWS. Import it with terraform import, use unique environment-prefixed names, or delete the orphaned job. Always check AWS Console and Terraform state to understand where the duplicate came from.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article