Created
March 21, 2023 23:29
-
-
Save Nicolas-Richard/61568a429b81a488a105d7c21e37d096 to your computer and use it in GitHub Desktop.
This script triggers and monitors an ASG refresh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ASG_NAME=$1 | |
| region=$2 | |
| profile=$3 | |
| export AWS_DEFAULT_REGION=$region | |
| export AWS_PROFILE=$profile | |
| # Start instance refresh with MinHealthyPercentage=100,InstanceWarmup=30 | |
| INSTANCE_REFRESH=$(aws autoscaling start-instance-refresh \ | |
| --auto-scaling-group-name $ASG_NAME \ | |
| --preferences MinHealthyPercentage=100,InstanceWarmup=30 \ | |
| --strategy Rolling \ | |
| --query "InstanceRefreshId" --output text) | |
| echo "Started instance refresh with ID: $INSTANCE_REFRESH" | |
| # Monitor instance refresh until refresh Status is Successful | |
| while true; do | |
| REFRESH_STATUS=$(aws autoscaling describe-instance-refreshes \ | |
| --auto-scaling-group-name $ASG_NAME \ | |
| --query "InstanceRefreshes[?InstanceRefreshId=='$INSTANCE_REFRESH'].Status[]" --output text) | |
| if [ "$REFRESH_STATUS" == "Successful" ]; then | |
| echo "Instance refresh completed successfully" | |
| exit 0 | |
| elif [ "$REFRESH_STATUS" == "Cancelled" ] || [ "$REFRESH_STATUS" == "Failed" ]; then | |
| echo "Instance refresh failed or was cancelled" | |
| exit 1 | |
| else | |
| REFRESH_INFO=$(aws autoscaling describe-instance-refreshes \ | |
| --auto-scaling-group-name $ASG_NAME \ | |
| --query "InstanceRefreshes[?InstanceRefreshId=='$INSTANCE_REFRESH'].[PercentageComplete,Status,StatusReason]" --output text) | |
| echo "Instance refresh status: $REFRESH_INFO" | |
| fi | |
| sleep 30 | |
| done | |
| unset AWS_DEFAULT_REGION | |
| unset AWS_PROFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment