Skip to content

Instantly share code, notes, and snippets.

@lavie
Last active August 27, 2018 07:35
Show Gist options
  • Save lavie/a829336ee53f6aecdbea8a365399e39c to your computer and use it in GitHub Desktop.
Save lavie/a829336ee53f6aecdbea8a365399e39c to your computer and use it in GitHub Desktop.
Bash script to replace all ASG instances (by flagging them as Unhealthy, ASG does the rest)
#!/bin/bash
# Usage `./replace-asg-instances.sh my-asg-name`
# Will set all instances to Unhealthy, so they are replaced by the ASG.
# Notice this is meant to work with EC2 health-check ASG, not ELB health-checks.
if [[ $1 == "" ]]; then
echo Usage "./replace-asg-instances.sh my-asg-name"
exit 1
fi
aws autoscaling describe-auto-scaling-groups | \
jq -r ".AutoScalingGroups[] | select (.AutoScalingGroupName | contains(\"$1\"))| .Instances[].InstanceId" | \
xargs -n1 -I {} \
aws autoscaling set-instance-health --cli-input-json '{"InstanceId": "{}", "HealthStatus": "Unhealthy"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment