Error: error deleting Batch Compute Environment : Cannot delete, found existing JobQueue relationship

Terraform version:- 0.12.8
Whenever we change the compute env template, we have to delete JobQueue as we get following error - “Error: error deleting Batch Compute Environment : Cannot delete, found existing JobQueue relationship”
Someone posted the following solution:

resource "random_string" "name" {
  length  = 16
  special = false
  upper   = false
}
resource "aws_batch_compute_environment" "this" {
  compute_environment_name = "${var.name}-${random_string.name.result}"
  # removed for brevity

  lifecycle {
    create_before_destroy = true
  }
}

This has resolved our issue, where we don’t have to delete JobQueue anymore, but as it’s creating a new compute env, every time, the old ones still exist. What is the best practice of cleaning up old compute env, do we need to run “terraform taint” every time we do an apply?

How are the job queues created?

I see a separate resources for that aws_batch_job_queue, is that what you are using? If you do that, using compute_environments = [aws_batch_compute_environment.this.arn] and Terraform fails to update the queue and replace the environment then I think it’s a bug which should be reported.

After reading through some forums, it seems it happens when you provide “compute_environment_name”.
We now have a workaround, where we are appending “timestamp” to the name. But now we have 2 problem

  1. compute env will be recreated every time we run apply, even if there are no changes to the compute resource.
  2. The older compute env is not getting deleted.
    Following is the workaround code snippet :

locals {
timestamp = formatdate(“YYYYMMDDhhmmss”, timestamp())
}

Create an AWS Batch Compute Environment

resource “aws_batch_compute_environment” “batch_compute_env” {
compute_environment_name = “somename_${local.timestamp}”
lifecycle {
create_before_destroy = true
}
}

This is the jobQueue resource creation

resource “aws_batch_job_queue” “batch_job_queue” {
name = “some_queue”
compute_environments = ["${aws_batch_compute_environment.batch_compute_env.arn}"]
}

Is anyone ever going to fix this issue?

It makes Terraform unusable for AWS Batch in a Production environment

3 Likes

Hi,

I just ran into the same issue and discovered this thread.
I found that using both the create_before_destroy lifecycle setting and configuring a compute_environment_name_prefix instead of a constant compute_environment_name appears to have solved the problem.

Terraform people. Please fix this. This is very annoying. The work around is messy.

I believe this is the GitHub issue representing the problem described by this topic:

You can record your interest in that issue by adding a :+1: upvote to its leading comment, which the AWS provider team uses as one of their inputs to prioritization.