Interview Prep/AWS/EC2, Auto Scaling and Load Balancing

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 →

  1. 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)
  2. Add a autoscaling:EC2_INSTANCE_TERMINATING lifecycle hook. The instance finishes or checkpoints its job, then calls complete-lifecycle-action
  3. 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.

aws All AWS questions Roadmap: AWS core services

More on EC2, Auto Scaling and Load Balancing