amazon-web-services - 使用 terraform 的 AWS ECS 容量提供商

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

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

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

最佳答案

请注意[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
    }
  }
}

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

AWS 错误(或设计)

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

ClientException: The specified capacity provider already exists.

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

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

$ 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
    }
}

关于amazon-web-services - 使用 terraform 的 AWS ECS 容量提供商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056801/

相关文章:

amazon-web-services - 正在寻找监控 ECS 部署失败通知的好方法?

Terraform 13,根据另一个值验证变量

amazon-web-services - AWS EC2 key 对中包含哪些信息?

mysql - 如何创建生产 RDS Aurora 实例的 "Write"克隆?

amazon-web-services - 如何在禁用集群模式的情况下从 aws_elasticache_replication_group 推断读取器端点地址

amazon-web-services - Terraform 和 AWS : modify an existing policy

terraform - 如何对外部程序隐藏 AWS 凭证?

amazon-web-services - 在 AWS Lambda 中存储单个值的最佳实践

amazon-web-services - 跨账户ECS部署

amazon-web-services - 如何使用 PHP 在 AWS 上启动任务 ECS