- Newest
- Most votes
- Most comments
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 😊
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:
-
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.
-
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.
-
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.
-
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.
-
Implement a "cool-down" period: Ensure that there's a sufficient delay between scaling actions to prevent rapid fluctuations in the number of tasks.
-
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
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/
Best,
Didier
Relevant content
asked 4 years ago
asked 3 years ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated 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?