简体   繁体   English

可以使用 Terraform 更新 AWS ECS 容量提供程序吗?

[英]Can AWS ECS capacity provider be updated using Terraform?

I was planning to provision Amazon ECS capacity provider via Terraform but I saw a few questions which talk about the updating of Amazon ECS capacity provider via Terraform was not possible.我计划通过 Terraform 提供Amazon ECS 容量提供程序,但我看到了一些问题,这些问题讨论了无法通过 Terraform 更新 Amazon ECS 容量提供程序。 Is the feature safe to be used now?现在可以安全使用该功能吗?

As of 21st June 2021, Terraform's AWS provider is currently unable to update ECS capacity providers.截至 2021 年 6 月 21 日,Terraform 的 AWS 提供商目前无法更新 ECS 容量提供商。

When ECS capacity providers were first introduced in 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.当 ECS 容量提供程序在v2.42.0中首次引入时,您无法通过 API 或控制台更新它们甚至删除它们,因此 将 ECS 容量提供程序添加到 Terraform AWS 提供程序的初始支持没有处理删除容量提供程序或允许更新标签以外的任何内容。 Deletion support was added in the later v2.67.0 release .在后来的v2.67.0 版本中添加了 删除支持

There's an open merge request that adds support for updating ECS capacity providers but it hasn't been merged and released yet.有一个 开放的合并请求,它增加了对更新 ECS 容量提供程序的支持,但尚未合并和发布。

Currently, when you attempt to update an ECS capacity provider Terraform will handle it like it does with all immutable resources and destroy the resource and recreate it.目前,当您尝试更新 ECS 容量提供程序时,Terraform 将像处理所有不可变资源一样处理它并销毁资源并重新创建它。 This can cause issues with Terraform because you can't delete a capacity provider that is in use by an ECS cluster (it will error with 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. ).这可能会导致 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. )。

To work around this you can use a destroy-time provisioner to detach the capacity provider from the ECS cluster which then allows Terraform to delete the capacity provider and create a new one.要解决此问题,您可以使用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,
  ]
}

You will then need to run terraform apply twice to have it then reattach the new capacity provider after the first run detaches it, deletes it and then creates the new capacity provider.然后,您需要运行terraform apply两次,然后在第一次运行将其分离、删除并创建新的容量提供程序后重新附加新的容量提供程序。

When the pull request to add support for in place updates is merged and released you can then remove the destroy-time provisioner and it will just work as expected, avoiding the need to run terraform apply twice.当添加对就地更新支持的拉取请求被合并并发布时,您可以删除 destroy-time provisioner,它将按预期工作,避免运行terraform apply两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM