Skip to content

ECS scale-down strategy for Azure DevOps agent pool

0

We are using AWS fargate instances for an Azure DevOps pool. We can scale up OK (not great, but OK), but scaling down seems to be a problem. We've had instances of AWS scaling down tasks that are currently running a job, which is certainly not ideal.

We've had to replace the metric alarm scaling with a lambda that ensures that when downsizing tasks, it has been idle for a period of time first. This is done by invoking Azure DevOps APIs and lining up idle agents with the task they are a part of.

Am I missing something with this? Is there something that I should be looking at instead of a scale-in lambda?

asked 2 years ago326 views

3 Answers
5

Hello,

By default, your ASG determines whether any of your instances are closest to the next billing hour before terminating them. You can find more details about the default termination policy in this documentation

If you'd like to use a different termination method, you can select one of the policies available in the Advanced configurations section of your ASG or implement a custom solution, such as the Lambda function you mentioned.

Another option is to enable Instance scale-in protection, which prevents specific instances from being terminated. However, keep in mind that if you activate this, the protected instances will never shut down automatically.

Additionally, when an instance is being terminated, your container will first receive a SIGTERM signal, giving you 30 seconds to gracefully stop processes in your program before a SIGKILL signal is sent. If you need more time, you can adjust this duration to fit your requirements.

hope this helps 😊

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Creating a custom configuration for an ASG sounds like an ideal solution, but that looks like it only applies to EC2 instances. Does it work with ECS Fargate instances?

1

You're on the right track with your approach to scaling down AWS Fargate instances for your Azure DevOps agent pool, but there are some additional considerations and potential improvements you could explore.

First, it's important to note that the built-in autoscaling for Amazon ECS (Elastic Container Service) with Fargate doesn't inherently understand the state of your Azure DevOps jobs. This is why you've encountered issues with tasks being scaled down while running jobs.

Your current approach using a Lambda function to check the idle state of agents before scaling down is a good start. This custom solution allows you to incorporate the specific needs of your Azure DevOps workload into the scaling decision.

However, there are a few things you could consider to potentially improve your setup:

  1. Use Amazon ECS Capacity Providers: These can help manage the scaling of your ECS tasks more effectively. They work with Auto Scaling groups to adjust capacity based on task demand.

  2. Implement a more sophisticated scaling strategy: Instead of relying solely on metric alarms, you could use a combination of CloudWatch metrics, custom metrics from your Azure DevOps API calls, and perhaps even Step Functions to orchestrate a more complex scaling process.

  3. Utilize managed scaling behavior: ECS offers managed scaling that can help optimize your cluster's scaling behavior. This includes features like minimum and maximum scaling step sizes, which can help prevent overly aggressive scaling.

  4. Consider using target tracking policies: These can automatically adjust the number of tasks based on a target value for a specific metric. While this might not directly solve your issue with running jobs, it could be part of a more comprehensive scaling strategy.

  5. Implement a "cool-down" period: Ensure that there's a sufficient delay between scaling actions to prevent rapid fluctuations in the number of tasks.

  6. Use task placement constraints: If you're using EC2 instances (not applicable to Fargate), you can use the "distinctInstance" constraint to ensure tasks are spread across instances, which can help with more gradual scaling.

Your current Lambda-based approach is not wrong, especially given the specific requirements of your Azure DevOps workload. However, combining it with some of these AWS-native features and best practices could lead to a more robust and efficient scaling solution. The key is to find the right balance between responsiveness to demand changes and stability for your running jobs.
Sources
Amazon ECS managed scaling behavior - Amazon Elastic Container Service
Optimize Amazon ECS cluster auto scaling - Amazon Elastic Container Service
Use a target metric to scale Amazon ECS services - Amazon Elastic Container Service

answered 2 years ago

EXPERT

reviewed 2 years ago

1

Hi,

I don't have a specific solution for your exact use case but this blog post may be useful for you to understand task termination in ECS and how to handle it gracefully: https://aws.amazon.com/ko/blogs/containers/graceful-shutdowns-with-ecs/

Enter image description here

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.