Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

Deploy to 10 servers behind an AWS ALB one at a time, removing each from the target group during its deploy. Outline the script.

AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Rolling deploy with traffic draining: deregister, wait for draining, deploy, health check, register, wait until healthy, next.

What I'd do →

for id in "${instance_ids[@]}"; do
  aws elbv2 deregister-targets --target-group-arn "$TG" --targets "Id=$id"
  aws elbv2 wait target-deregistered --target-group-arn "$TG" --targets "Id=$id"
  ip=$(aws ec2 describe-instances --instance-ids "$id" --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text)
  ssh -o BatchMode=yes "deploy@$ip" "sudo /opt/deploy/deploy.sh $(printf '%q' "$version")" ||
    { echo "deploy failed on $id, stopping rollout" >&2; exit 1; }
  aws elbv2 register-targets --target-group-arn "$TG" --targets "Id=$id"
  aws elbv2 wait target-in-service --target-group-arn "$TG" --targets "Id=$id" ||
    { echo "$id never became healthy, stopping rollout" >&2; exit 1; }
done
  1. The built-in wait commands poll with sensible timeouts.
  2. Stop on the first failure; leave the failed instance out of the target group for investigation.
  3. Check capacity first: with 10 instances, losing one at a time must not overload the rest.

Why → Draining avoids dropped requests; stopping early limits the blast radius.

Interview tip → Mention the target group's deregistration delay (default 300 s) and tuning it for faster rollouts.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations