我正在尝试通过 terraform 模板在 ASG 内部署 ECS Fargate 配置。据我所知,AWS ASG 识别两种扩展方法:
目标跟踪扩展策略:这些策略会根据需要自动调整可扩展目标的数量,以将您选择的指标保持在所选目标值。
步进扩展策略:这些策略根据一组扩展调整(称为步进调整)增加或减少资源的当前容量,这些调整根据警报违规的大小而变化。
但是,当尝试根据以下代码将我的 ASG 资源配置为扩展时,使用以平均 CPU 利用率作为指标的目标跟踪:
resource "aws_appautoscaling_target" "ecs_target" {
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${var.service_name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 1
max_capacity = 10
}
resource "aws_appautoscaling_policy" "ecs_cpu_policy" {
name = "cpu-scaling"
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 50.0
scale_in_cooldown = 300
scale_out_cooldown = 300
}
}
我在 terraform apply 上收到以下错误:
│ Error: creating Application Auto Scaling Policy (cpu-scaling): ValidationException: StepScaling policy configuration must be specified
果然,更改为使用逐步扩展策略确实允许我进行部署。为什么一种方法成功了,而另一种方法却没有通过 Terraform 获得 AWS 认可?
阅读此资源的 docs,我看到的示例定义了
policy_type
resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
...
policy_type = "TargetTrackingScaling"
...
target_tracking_scaling_policy_configuration {
看起来默认的policy_type是StepScaling
resource "aws_appautoscaling_policy" "ecs_cpu_policy" {
name = "cpu-scaling"
policy_type = "TargetTrackingScaling"
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 50.0
scale_in_cooldown = 300
scale_out_cooldown = 300
}
}