Last active
May 30, 2017 13:40
-
-
Save geronimo-iia/c6e834884a2fbe2349e0b49686e34090 to your computer and use it in GitHub Desktop.
find AWS EMR on tag name/value and terminate it
This file contains 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 | |
# Remove termination protection and terminate cluster | |
# params: | |
# $1: clusterId cluster identifier | |
# | |
terminateCluster() { | |
clusterId=$1 | |
aws emr modify-cluster-attributes --cluster-id ${clusterId} --no-termination-protected | |
aws emr terminate-clusters --cluster-ids ${clusterId} | |
} | |
# Find clusters on tag name/value. | |
# params: | |
# $1: tag name ('Name' per default) | |
# $2: tag value ('mycluster per default) | |
# return a list of cluster idendentifiers | |
findCluster() { | |
key=${1:-Name} | |
value=${2:-mycluster} | |
result=() | |
for clusterId in $(aws emr list-clusters --active | jq -r '.Clusters | .[] | .Id') | |
do | |
matchedId=$(aws emr describe-cluster --cluster-id ${clusterId} | jq -r --arg key "$key" --arg value "$value" '.Cluster | select( (.Tags | .[] ) | .Key == $key and .Value == $value) | .Id ') | |
[ ! -z "$matchedId" ] && result+=(${matchedId}) | |
done | |
echo ${result} | |
} | |
for clusterId in $(findCluster $1 $2) | |
do | |
echo "Terminate cluster ${clusterId}" | |
terminateCluster ${clusterId} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment