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
- The built-in
waitcommands poll with sensible timeouts. - Stop on the first failure; leave the failed instance out of the target group for investigation.
- 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.
More on Scenarios: deployments, user management and fleet operations
Write the core of a deploy script with release directories, an atomic symlink switch and rollback.IntermediateAfter a deploy, the health check fails intermittently. Your script must roll back automatically, but only if the failure persists. How?IntermediateImplement a blue/green switch on a single nginx host with a script.AdvancedOnboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.IntermediateAn engineer leaves the company. Write the offboarding steps for Linux servers.IntermediateYou are asked to audit a server for risky accounts. What does your script check?Intermediate