During scale-in, the ASG terminates instances in the middle of long batch jobs.
AdvancedTroubleshootingEC2, Auto Scaling and Load Balancing
Answer
Likely causes → Scale-in picks instances using the termination policy and knows nothing about the work on them. Without lifecycle hooks or protection, jobs are killed.
Diagnose →
aws autoscaling describe-scaling-activities --auto-scaling-group-name workers --max-items 10
aws autoscaling describe-lifecycle-hooks --auto-scaling-group-name workers
aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[].[InstanceId,ProtectedFromScaleIn]'
Fix →
- The worker sets scale-in protection while it is busy:
aws autoscaling set-instance-protection --instance-ids i-0abc --auto-scaling-group-name workers --protected-from-scale-in(and clears it when done) - Add a
autoscaling:EC2_INSTANCE_TERMINATINGlifecycle hook. The instance finishes or checkpoints its job, then callscomplete-lifecycle-action - Scale on queue depth per instance instead of CPU
Prevent → Design jobs to be idempotent and checkpointed, and size the hook HeartbeatTimeout to the longest job.