使用 terraform 的 AWS ECS 容量提供程序



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

(我使用的是提供商版本:"2.45.0"( 谢谢!

请注意 [弹性云服务器] 添加删除 ASG 容量提供程序的功能。 #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 错误(或设计(

但是,在terrafom销毁后,当尝试运行terraform时,再次应用:

ClientException: The specified capacity provider already exists.

一旦在这种情况下失败,可能需要在 Terraform 脚本中禁用容量提供程序(似乎删除了容量提供程序资源,但实际上由于 AWS 错误,它仍然存在(。

因此,解决方法可能是使用 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
}
}

为了创建新资源,还需要向ecs_cluster模块添加一个新参数:"capacity_providers"。

相关内容

  • 没有找到相关文章

最新更新