Terraform:如何启用批处理服务计算环境的删除?

Terraform: How to enable deletion of batch service compute environment?

我已经使用 Terraform 建立了基础设施,包括批处理服务作业队列、计算环境和作业定义。

对 Terraform 进行更改后,我有 运行 terraform apply 并收到以下错误:

Error: error deleting Batch Compute Environment (data-load): : Cannot delete, found existing JobQueue relationship
    status code: 400, request id: 25449415-9c36-4748-95e6-925647bd716a

作业队列中没有作业。我假设它会 removed/replaced 以及与批处理服务相关的其他资源,而不是在被替换时阻止计算环境的显示。

在过去,我能克服这个问题的唯一方法是破坏我的状态文件并重新开始,但我认为一定有更好的方法。我怎样才能解决这个问题?

在Terraform 中重新创建资源时,默认按顺序删除和创建。所以如果compute_environment_nameyou改变只应用,job队列暂时依赖的计算环境是不存在的,所以你会死如下。 错误:应用计划时出错:


1 error(s) occurred:

* aws_batch_compute_environment.sample (destroy): 1 error(s) occurred:

* aws_batch_compute_environment.sample: error deleting Batch Compute Environment (sample): : Cannot delete, found existing JobQueue relationship

因此,compute_environment_namechange create_before_destroy = true 并明确指定生命周期。


resource "aws_batch_compute_environment" "sample" {
  compute_environment_name = "sample-v2"
                     ...
    instance_type = [
      "m5",
    ]
                     ...
  lifecycle {
    create_before_destroy = true
  }
}

我通过添加 create_before_destroy 生命周期设置并使用 compute_environment_name_prefix 而不是常量 compute_environment_name 解决了这个问题。如下:

resource "aws_batch_compute_environment" "sample" {
  compute_environment_name_prefix = var.compute_env_name

  lifecycle {
    create_before_destroy = true
  }

  compute_resources {
    ...
  }
}