1
votes

I am trying to implement schedule auto scaling using Terraform with Auto Scaling in AWS Fargate cluster service.

My requirement is I want to scale-out my ECS service 7 am UST and scale in 7 pm UST. For scale out ECS service I am using scheduled scaling in Terraform its working fine. But my concern here is how can scale in using same logic or different logic. Here I tried using different logic for scale in and scale out. When I use both logics when I apply terraform apply its giving error like

Error: ConcurrentUpdateException: You already have a pending update to an Auto Scaling resource.

I am successfully auto scale my ECS services using below code on a particular time. Now can I scale my ECS service on a particular time?

resource "aws_appautoscaling_target" "my_service" {
  max_capacity       = var.my_service-max_count
  min_capacity       = var.my_service-count
  resource_id        = "service/mycluster/${aws_ecs_service.my_service.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}
resource "aws_appautoscaling_scheduled_action" "my_service" {
  name               = "my_service"
  service_namespace  = "${aws_appautoscaling_target.my_service.service_namespace}"
  resource_id        = "${aws_appautoscaling_target.my_service.resource_id}"
  scalable_dimension = "${aws_appautoscaling_target.my_service.scalable_dimension}"
  schedule           = "cron(0 7 * * ? *)"

  scalable_target_action {
    min_capacity = 2
    max_capacity = 10
  }
}
resource "aws_appautoscaling_scheduled_action" "my_service_scale_out" {
  name               = "my_service"
  service_namespace  = "${aws_appautoscaling_target.my_service.service_namespace}"
  resource_id        = "${aws_appautoscaling_target.my_service.resource_id}"
  scalable_dimension = "${aws_appautoscaling_target.my_service.scalable_dimension}"
  schedule           = "cron(0 19 * * ? *)"

  scalable_target_action {
    min_capacity = 1
    max_capacity = 1
  }
}
1

1 Answers

1
votes

I was running into this exact same problem and then found the solution here: https://keita.blog/2018/01/29/aws-application-auto-scaling-for-ecs-with-terraform/

Just add:

depends_on = ["aws_appautoscaling_scheduled_action.my_service"]

in your resource my_service_scale_out