【问题标题】:AWS ECS capacity provider using terraform使用 terraform 的 AWS ECS 容量提供程序
【发布时间】:2020-05-20 05:54:53
【问题描述】:

我正在尝试向我现有的由 terraform 管理的基础架构添加 ECS 集群的容量提供程序。 Terraform 应用返回没有错误,新资源已添加到状态文件中,但令人惊讶的是它没有出现在 AWS GUI 中(ECS 集群-> 容量提供程序-> 无结果)。 如果我使用 aws cli 很好地列出此资源输出,那么重建所有内容也无济于事。 有没有人成功使用 terraform 为 ECS 添加容量提供程序?

(我正在使用提供程序版本:“2.45.0”) 谢谢!

【问题讨论】:

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


    【解决方案1】:

    请注意[ECS] Add the ability to delete an ASG capacity provider. #632。创建后不能删除,只能更新。

    resource "aws_ecs_cluster" "this" {
      name = "${var.PROJECT}_${var.ENV}_${local.ecs_cluster_name}"
    
      # List of short names of one or more capacity providers
      capacity_providers = local.enable_ecs_cluster_auto_scaling == true ? aws_ecs_capacity_provider.asg[*].name : []
    }
    
    resource "aws_ecs_capacity_provider" "asg" {
      count = local.enable_ecs_cluster_auto_scaling ? 1 : 0
    
      name = "${var.PROJECT}-${var.ENV}-ecs-cluster-capacity-provider"
    
      auto_scaling_group_provider {
        auto_scaling_group_arn         = local.asg_ecs_cluster_arn
    
        #--------------------------------------------------------------------------------
        # When using managed termination protection, managed scaling must also be used otherwise managed termination protection will not work.
        # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-capacity-providers.html#capacity-providers-considerations
        # Otherwise Error:
        # error creating capacity provider: ClientException: The managed termination protection setting for the capacity provider is invalid.
        # To enable managed termination protection for a capacity provider, the Auto Scaling group must have instance protection from scale in enabled.
        #--------------------------------------------------------------------------------
        managed_termination_protection = "ENABLED"
    
        managed_scaling {
          #--------------------------------------------------------------------------------
          # Whether auto scaling is managed by ECS. Valid values are ENABLED and DISABLED.
          # When creating a capacity provider, you can optionally enable managed scaling.
          # When managed scaling is enabled, ECS manages the scale-in/out of the ASG.
          #--------------------------------------------------------------------------------
          status                    = "ENABLED"
          minimum_scaling_step_size = local.ecs_cluster_autoscaling_min_step_size
          maximum_scaling_step_size = local.ecs_cluster_autoscaling_max_step_size
          target_capacity           = local.ecs_cluster_autoscaling_target_capacity
        }
      }
    }
    

    这行得通,并确认 Auto Scaling 由于资源使用率低而减少了 EC2 实例,并且服务任务(docker 容器)被重新定位到正在运行的 EC2 实例。

    AWS 错误(或设计)

    但是,在 terrafom destroy 之后,尝试再次运行 terraform apply 时:

    ClientException: The specified capacity provider already exists.
    

    一旦遇到这种情况,可能需要在 Terraform 脚本中禁用容量提供程序(看起来会删除容量提供程序资源,但实际上由于 AWS 错误它仍然存在)。

    因此,可能的解决方法是使用 CLI 将不可变容量提供程序添加到集群,提供容量提供程序指向的 Auto Scaling 组仍然存在。

    $ CAPACITY_PROVIDER=$(aws ecs describe-capacity-providers | jq -r '.capacityProviders[] | select(.status=="ACTIVE" and .name!="FARGATE" and .name!="FARGATE_SPOT") | .name')
    $ aws ecs put-cluster-capacity-providers --cluster YOUR_ECS_CLUSTER --capacity-providers ${CAPACITY_PROVIDERS} --default-capacity-provider-strategy capacityProvider=${CAPACITY_PROVIDER},base=1,weight=1
    
    {
        "cluster": {
            "clusterArn": "arn:aws:ecs:us-east-2:200506027189:cluster/YOUR_ECS_CLUSTER",
            "clusterName": "YOUR_ECS_CLUSTER",
            "status": "ACTIVE",
            "registeredContainerInstancesCount": 0,
            "runningTasksCount": 0,
            "pendingTasksCount": 0,
            "activeServicesCount": 0,
            "statistics": [],
            "tags": [],
            "settings": [
                {
                    "name": "containerInsights",
                    "value": "disabled"
                }
            ],
            "capacityProviders": [
                "YOUR_CAPACITY_PROVIDER"
            ],
            "defaultCapacityProviderStrategy": [
                {
                    "capacityProvider": "YOUR_CAPACITY_PROVIDER",
                    "weight": 1,
                    "base": 1
                }
            ],
            "attachments": [
                {
                    "id": "628ee192-4d0f-44be-85c0-049d796ed65c",
                    "type": "asp",
                    "status": "PRECREATED",
                    "details": [
                        {
                            "name": "capacityProviderName",
                            "value": "YOUR_CAPACITY_PROVIDER"
                        },
                        {
                            "name": "scalingPlanName",
                            "value": "ECSManagedAutoScalingPlan-89682dcf-bb53-492f-8329-25d75458ea11"
                        }
                    ]
                }
            ],
            "attachmentsStatus": "UPDATE_IN_PROGRESS"      <----- Takes time for the capacity provider to show up in ECS clsuter console
        }
    }
    

    【讨论】:

    • 我想澄清一下 - 您不能在创建容量后删除 *也不能更新* - 请参阅 github.com/aws/containers-roadmap/issues/633
    • 臭名昭著的 ECS 不可删除容量提供程序问题现已修复:aws.amazon.com/about-aws/whats-new/2020/06/…
    • 我在尝试使用 terraform 与容量提供程序创建集群时收到以下错误。等待 ECS 集群创建时出错:意外状态“失败”,想要目标“活动”。最后一个错误:%!s()
    【解决方案2】:

    要创建新资源,还需要在 ecs_cluster 模块中添加一个新参数:“capacity_providers”。

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 2020-12-11
      • 1970-01-01
      • 2020-05-29
      • 2022-01-04
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      相关资源
      最近更新 更多