-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-nuke-gateways.sh
executable file
·46 lines (43 loc) · 1.43 KB
/
aws-nuke-gateways.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# Parse arguments:
while getopts ":r:p:v" opt; do
case $opt in
r) REGION="--region $OPTARG";;
p) PROFILE="--profile $OPTARG";;
v) VERBOSE=$(( VERBOSE + 1 ));;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
\?) echo "Invalid option: -$OPTARG" >&2;;
esac
done
shift $((OPTIND-1))
function setval { printf -v "$1" "%s" "$(cat)"; declare -p "$1"; }
function myaws () {
local stdval
local errval
local retval
local retrycount=4
while [[ $retrycount -gt 0 ]] ; do
retrycount=$(( retrycount -1 ))
eval "$( aws $PROFILE $REGION "$@" 2> >(setval errval) > >(setval stdval); <<<"$?" setval retval; )"
#echo "std out is : |$stdval| std err is : |$errval| return val is : |$retval|"
# Success:
if [[ "$retval" == 0 ]]; then echo "$stdval"; return 0; fi
# Throttled:
if [[ "$errval" =~ RequestLimitExceeded ]]; then echo "$errval" 1>&2; continue; fi
# Other error:
echo "$stdval"
echo "$errval" >&2
return "$retval"
done
echo "Too many retries, giving up." >&2
exit
}
function ec2 () { myaws ec2 "$@"; }
function listgateways () { ec2 describe-internet-gateways | jq -r '.InternetGateways[] | .InternetGatewayId'; }
function deletegateway () { ec2 delete-internet-gateway --internet-gateway-id "$1"; }
if ! gws=$(listgateways); then exit 1; fi
echo "found these gateways, trying to delete them: $gws"
for n in $gws; do
echo Deleting $n
deletegateway $n
done