Skip to content

Instantly share code, notes, and snippets.

@vaijab
Created January 19, 2016 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaijab/570546d7516affbd44fc to your computer and use it in GitHub Desktop.
Save vaijab/570546d7516affbd44fc to your computer and use it in GitHub Desktop.
aws cli: retry on RequestLimitExceeded error
#!/usr/bin/bash
function retry() {
tries=0
max_tries=3
sleep_sec=1
exit_code=256
error=''
until { error=$(${@} 2>&1 1>&${stdout}); } {stdout}>&1; do
exit_code=$?
tries=$(( ${tries} + 1 ))
if [[ ${tries} -gt ${max_tries} ]]; then
exit ${exit_code}
fi
if [[ ${exit_code} == 255 ]] && (echo "${error}" | grep -q 'RequestLimitExceeded'); then
if [[ ${tries} != 1 ]]; then
sleep ${sleep_sec}
fi
sleep_sec=$((${sleep_sec} * 2))
echo "${error}" >&2
echo 'Being throttled. Retrying..' >&2
else
echo "${error}" >&2
exit ${exit_code}
fi
done
}
retry $@
@vaijab
Copy link
Author

vaijab commented Jan 19, 2016

./aws_limit_exceeded_retry.sh aws ec2 describe-instances

A server error (RequestLimitExceeded) occurred when calling the DescribeInstances operation: Request limit exceeded.
Being throttled. Retrying..

A server error (RequestLimitExceeded) occurred when calling the DescribeInstances operation: Request limit exceeded.
Being throttled. Retrying..

@ianbamforth
Copy link

Nice, a couple of potential improvements though:

  1. quote the ${@} so that arguments with spaces in (e.g. aws ec2 create-tags --tags Key=type,Value="my type" ... ) will work
  2. when calling from a script, might not want to exit that script (i.e. let the caller deal with the aftermath of failure) - putting a subshell in here (i.e. wrap in parentheses) would help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment