Skip to content

Instantly share code, notes, and snippets.

@geronimo-iia
Last active May 30, 2017 13:40
Show Gist options
  • Save geronimo-iia/c6e834884a2fbe2349e0b49686e34090 to your computer and use it in GitHub Desktop.
Save geronimo-iia/c6e834884a2fbe2349e0b49686e34090 to your computer and use it in GitHub Desktop.
find AWS EMR on tag name/value and terminate it
#!/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