How to Update Minimum & Maximum Running Tasks in ECS

amazon-ecsamazon-web-services

I want to update the min and max number of tasks for a service in ECS in a lambda function (python with boto3). It looks like the closest thing is the desired count: desiredCount, but I don't think that is what I am after as my auto-scaling policy will change this anyway.

A bit more about my use case.

I have some containers on ECS of which poll a queue for jobs, and I to have some number of these always running during working hours, but then none running out of working hours. When load comes into the system my auto-scaling policy will scale out containers but a user would have to wait longer for this to happen when there are no containers, hence I want some always on during working hours.

I am also using AWS Fargate.

Best Answer

I guess you'll have to do it in 2 steps, using application auto-scaling boto3 client:

  1. Create your ECS service as Application auto-scaling target with register_scalable_target() with for example MinCapacity=3 and MaxCapacity=100. This call will return the scalable targer ARN.

  2. Every evening call register_scalable_target() again with MinCapacity=0 and every business day morning again with MinCapacity=3.

See the register_scalable_target() documentation where it explicitly says:

After you have registered a scalable target, you can use this operation to update the minimum and maximum values for its scalable dimension.

Your auto-scaling policy will then set DesiredCapacity within the Min/MaxCapacity boundaries where the MinCapacity will change from 0 to 3 during business hours and back to 0 after hours.

Hope that helps :)

Related Topic