#bash #aws #retry awscli retry backoff for bash
#!/usr/bin/env bash | |
set -e | |
aws_retry() { | |
last_aws_call="" | |
local max_attempts=${ATTEMPTS-8} | |
local timeout=${TIMEOUT-2} | |
local attempt=0 | |
local exitCode=0 | |
while [[ $attempt < $max_attempts ]] | |
do | |
echo "Attempt - $attempt" 1>&2 | |
set +e | |
last_aws_call=$(${@}) | |
exitCode=$? | |
set -e | |
if [[ ! $last_aws_call =~ .*RequestLimitExceeded.* ]] && [[ $exitCode == 0 ]]; then | |
echo "awscli command ok" 1>&2 | |
echo "exitCode zero on: $@" 1>&2 | |
break | |
fi | |
echo "Failure! Retrying in $timeout.." 1>&2 | |
echo "Retry command: $@" 1>&2 | |
sleep $timeout | |
attempt=$(( attempt + 1 )) | |
timeout=$(( timeout * 2 )) | |
done | |
if [[ $exitCode != 0 ]]; then | |
echo "You've failed me for the last time! ($@)" 1>&2 | |
exit $exitCode | |
fi | |
} | |
main() { | |
aws_retry aws ec2 describe-instances ... | |
echo $last_aws_call | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment