AWS - EB Detach Deploy
Updated at 2016-01-21 00:37
Deploying to Elastic Beanstalk halts processing but sometimes you want to deploy new version while a long running worker is still processing.
# space separated list of instances you want to keep alive
INSTANCES_TO_KEEP="i-aaaaaa i-bbbbbb i-cccccc"
# ASG will be our auto scaling group name based on the first instance
ASG=$(aws autoscaling describe-auto-scaling-instances \
--instance-ids $(echo $INSTANCES_TO_KEEP | head -n1 | cut -d " " -f1) \
--query 'AutoScalingInstances[].AutoScalingGroupName' \
--output text)
# Enable instance protection if it's not on:
aws autoscaling set-instance-protection \
--instance-ids $INSTANCES_TO_KEEP \
--auto-scaling-group-name $ASG \
--protected-from-scale-in
# Make sure group min size is 0 so it doesn't start new instances.
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name $ASG \
--min-size 0 \
--desired-capacity $(echo $INSTANCES_TO_KEEP | wc -w | tr -d " ")
# Detach instances while lowering desired capacity.
aws autoscaling detach-instances \
--instance-ids $INSTANCES_TO_KEEP \
--auto-scaling-group-name $ASG \
--should-decrement-desired-capacity
eb deploy environment_name
# Attaching instances will automatically increase desired capacity.
aws autoscaling attach-instances \
--instance-ids $INSTANCES_TO_KEEP \
--auto-scaling-group-name $ASG
# Re-enable instance protection if it was on.
aws autoscaling set-instance-protection \
--instance-ids $INSTANCES_TO_KEEP \
--auto-scaling-group-name $ASG \
--protected-from-scale-in
Sometimes you might also have instances you just want to remove, you can execute this after setting min and desired for the group.
# space separated list of instances you want to terminate
INSTANCES_TO_TERMINATE="i-111111 i-222222 i-333333"
# Terminate useless instances
aws ec2 terminate-instances --instance-ids $INSTANCES_TO_TERMINATE
Source
- Raimo