【问题标题】:Terraform doesn't delete auto-scaling group with scale-in protection enabledTerraform 不会删除启用了缩减保护的自动缩放组
【发布时间】:2021-09-07 22:14:36
【问题描述】:

Terraform 在打开保护缩放时无法销毁自动缩放组,是否有任何解决方法?

尝试使用此功能,但在 AWS 控制台中自动缩放组的活动部分下,我看到它已被取消,因为启用了缩减保护。

provisioner "local-exec" {
    when    = destroy
    command = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${self.name} --min-size 0 --desired-capacity 0"

  }

【问题讨论】:

  • 您是否在实例上使用终止生命周期挂钩?
  • 你能解释一下吗,我不知道这个。此 ASG 用于我的 ECS 部署的容量提供程序

标签: amazon-web-services terraform amazon-ecs terraform-provider-aws


【解决方案1】:

您可以使用 aws_autoscaling_group 资源上的 force_delete argument 来删除 ASG,而无需等待实例终止。 AWS Go SDK 文档上的This comment 对此进行了更多解释:

    // Specifies that the group is to be deleted along with all instances associated
    // with the group, without waiting for all instances to be terminated. This
    // parameter also deletes any outstanding lifecycle actions associated with
    // the group.
    ForceDelete *bool `type:"boolean"`

请注意,如果您依赖于 autoscaling group lifecycle hooks(例如 termination lifecycle hook that drains a container instance of any ECS tasks before the instance is terminated),则会跳过这些。

如果您依赖终止生命周期挂钩,那么您可以改为使用销毁时间预置器来像您已经尝试过的那样使用 AWS CLI,但使用 aws autoscaling set-instance-protection command 删除保护规模:

resource "aws_autoscaling_group" "autoscaling_group" {
  # ...

  provisioner "local-exec" {
    when    = destroy
    command = <<EOF
      AUTOSCALING_INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names ${self.name} --query 'AutoScalingGroups[0].Instances[].InstanceId' --output text)
      aws autoscaling set-instance-protection --auto-scaling-group-name ${self.name} --instance-ids "$${AUTOSCALING_INSTANCE_IDS}" --no-protected-from-scale-in
    EOF

  }
}

【讨论】:

  • 我在 terraform 销毁期间从控制台输出中看到,Terraform 首先尝试销毁 ECS 集群,然后等待 10 分钟,然后显示:错误:删除 ECS 集群时出错:ClusterContainsContainerInstancesException:The当容器实例处于活动状态或耗尽时,无法删除集群。 Terraform 版本详细信息:Terraform v0.14.7 + 提供者 registry.terraform.io/hashicorp/aws v3.46.0
  • 您需要显示更多代码才能显示该问题,但看起来 Terraform 的破坏图错误地尝试在 ASG 之前删除 ECS 集群,由于该错误而无法执行此操作.您仍然需要 force_delete 或销毁时间配置程序来删除受保护实例中的任何规模(例如,使用容量提供程序时),但您现在的问题更多是关于 Terraform 销毁事物的顺序。
  • 我建议将其拆分为另一个问题,这样它就不会成为一个多问题问题,并对其进行编辑以专门用于删除具有保护范围的 ASG。
  • 创建了一个单独的问题link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2019-12-17
  • 2017-10-20
  • 2019-12-30
  • 2021-06-12
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多