amazon-web-services - 使用 terraform 的 AWS ECS 容量提供程序
问题描述
我正在尝试将 ECS 集群的容量提供程序添加到由 terraform 管理的现有基础架构中。Terraform 应用返回没有错误,新资源已添加到状态文件中,但令人惊讶的是它没有出现在 AWS GUI 中(ECS 集群-> 容量提供程序-> 无结果)。如果我使用 aws cli 很好地列出此资源输出,那么重建所有内容也无济于事。有没有人成功地使用 terraform 为 ECS 添加容量提供程序?
(我使用的是提供者版本:“2.45.0”)谢谢!
解决方案
请注意[ECS] 添加删除 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
}
}
}
由于资源使用率低,这有效并确认了 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
}
}
推荐阅读
- c++ - long long int 中的溢出
- django - 如何使用 DRF + JWT 验证/保护非 api_views,例如。呈现私人页面
- css - 固定元素行为不如预期
- reactjs - React 基于角色的授权
- javascript - 如何在 Visual Studio 中获得 Jslib 文件的语法高亮显示
- c++ - (C++)数组作为结构的成员:如何获得它的大小?
- python - Python:源代码具有无法从外部访问的属性
- amazon-web-services - Cloudfront 路径作为代理
- linux - 为什么不能将新添加的文件推送到远程存储库?
- javascript - 我想显示我在grapesjs 编辑器中创建的这个html 内容