首页 > 解决方案 > Terraform 在 terraform 之外手动更改自动缩放组/实例组

问题描述

我有使用 Terraform 创建的基础设施。根据云提供商的不同,会有一些自动扩展组 (AWS) 或实例组 (GCP)。它不直接管理实例。

我想编写一个 AWS 或 gcloud 命令来将自动缩放减少到最小 0,最大 0,删除运行状况检查,基本上是与实例组相关的所有其他内容。然后等待组删除所有实例。然后我手动将所有内容更改回原始设置。

标签: google-cloud-platformterraform-provider-awsterraform-provider-gcp

解决方案


你确实可以做到这一点。然而,IaC 工具专注于“期望的状态”。通过更改扩展/消耗资源,您可以在 Terraform 之外执行此操作。然后,在确定您的资源已耗尽后,让 Terraform 将基础设施恢复到所需的状态。

类似但仍使用 IaC 的东西将 terraform local 定义stateslocal变量。然后,来回切换状态。

例子:

# define the states
locals {
   instance_state { 
      draining {
         min_instances = 0
         max_instances = 0
         health_check = false
      }

      black_friday {
         min_instances = 20
         max_instances = 100
         health_check = true
      }

      default {
         min_instances = 5
         max_instances = 20
         health_check = true
      }
   }
}

# You set the state here...
locals {
   desired_state = local.instance_state.draining
}

然后,在您的资源中使用desired_state

# No change needed here since it points to the desired_state
resource "type_some_resource" "resource_name" {
   min_instances = local.desired_state.min_instances
   max_instances = local.desired_state.max_instances
   health_check  = local.desired_state.health_check
}

推荐阅读