可以使用 Terraform 更新 AWS ECS 容量提供程序吗?
Can AWS ECS capacity provider be updated using Terraform?
我打算通过 Terraform 提供 Amazon ECS capacity provider,但我看到一些问题,这些问题讨论了无法通过 Terraform 更新 Amazon ECS 容量提供程序。该功能现在可以安全使用吗?
截至 2021 年 6 月 21 日,Terraform 的 AWS 提供商目前无法更新 ECS 容量提供商。
在 v2.42.0 you couldn't update them or even delete them via the API or console and so the initial support that added ECS capacity providers to the Terraform AWS provider didn't handle deleting capacity providers or allow updating anything other than the tags. Deletion support was added in the later v2.67.0 release 中首次引入 ECS 容量提供程序时。
有一个 open merge request 添加了对更新 ECS 容量提供程序的支持,但尚未合并和发布。
目前,当您尝试更新 ECS 容量提供程序时,Terraform 将像处理所有不可变资源一样处理它并销毁资源并重新创建它。这可能会导致 Terraform 出现问题,因为您无法删除 ECS 集群正在使用的容量提供程序(它将出错 The capacity provider cannot be deleted because it is associated with cluster: $CAPACITY_PROVIDER_NAME. Remove the capacity provider from the cluster and try again.
)。
要解决此问题,您可以使用 destroy-time provisioner 从 ECS 集群中分离容量提供程序,然后允许 Terraform 删除容量提供程序并创建一个新的。
resource "aws_autoscaling_group" "test" {
# ... other configuration, including potentially other tags ...
tag {
key = "AmazonECSManaged"
value = ""
propagate_at_launch = true
}
}
resource "aws_ecs_capacity_provider" "capacity_provider" {
name = var.cluster_name
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.test.arn
managed_termination_protection = "ENABLED"
managed_scaling {
maximum_scaling_step_size = 1000
minimum_scaling_step_size = 1
status = "ENABLED"
target_capacity = 10
}
}
provisioner "local-exec" {
when = destroy
command = "aws ecs put-cluster-capacity-providers --cluster ${self.name} --capacity-providers [] --default-capacity-provider-strategy []"
}
}
resource "aws_ecs_cluster" "cluster" {
name = var.cluster_name
capacity_providers = [
aws_ecs_capacity_provider.capacity_provider.name,
]
}
然后您将需要 运行 terraform apply
两次,然后在第一次 运行 分离、删除并创建新的容量提供程序后重新连接新的容量提供程序.
当添加对就地更新支持的拉取请求被合并并发布时,您可以删除 destroy-time provisioner,它将按预期工作,避免需要 运行 terraform apply
两次。
我打算通过 Terraform 提供 Amazon ECS capacity provider,但我看到一些问题,这些问题讨论了无法通过 Terraform 更新 Amazon ECS 容量提供程序。该功能现在可以安全使用吗?
截至 2021 年 6 月 21 日,Terraform 的 AWS 提供商目前无法更新 ECS 容量提供商。
在 v2.42.0 you couldn't update them or even delete them via the API or console and so the initial support that added ECS capacity providers to the Terraform AWS provider didn't handle deleting capacity providers or allow updating anything other than the tags. Deletion support was added in the later v2.67.0 release 中首次引入 ECS 容量提供程序时。
有一个 open merge request 添加了对更新 ECS 容量提供程序的支持,但尚未合并和发布。
目前,当您尝试更新 ECS 容量提供程序时,Terraform 将像处理所有不可变资源一样处理它并销毁资源并重新创建它。这可能会导致 Terraform 出现问题,因为您无法删除 ECS 集群正在使用的容量提供程序(它将出错 The capacity provider cannot be deleted because it is associated with cluster: $CAPACITY_PROVIDER_NAME. Remove the capacity provider from the cluster and try again.
)。
要解决此问题,您可以使用 destroy-time provisioner 从 ECS 集群中分离容量提供程序,然后允许 Terraform 删除容量提供程序并创建一个新的。
resource "aws_autoscaling_group" "test" {
# ... other configuration, including potentially other tags ...
tag {
key = "AmazonECSManaged"
value = ""
propagate_at_launch = true
}
}
resource "aws_ecs_capacity_provider" "capacity_provider" {
name = var.cluster_name
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.test.arn
managed_termination_protection = "ENABLED"
managed_scaling {
maximum_scaling_step_size = 1000
minimum_scaling_step_size = 1
status = "ENABLED"
target_capacity = 10
}
}
provisioner "local-exec" {
when = destroy
command = "aws ecs put-cluster-capacity-providers --cluster ${self.name} --capacity-providers [] --default-capacity-provider-strategy []"
}
}
resource "aws_ecs_cluster" "cluster" {
name = var.cluster_name
capacity_providers = [
aws_ecs_capacity_provider.capacity_provider.name,
]
}
然后您将需要 运行 terraform apply
两次,然后在第一次 运行 分离、删除并创建新的容量提供程序后重新连接新的容量提供程序.
当添加对就地更新支持的拉取请求被合并并发布时,您可以删除 destroy-time provisioner,它将按预期工作,避免需要 运行 terraform apply
两次。