Skip to content

Instantly share code, notes, and snippets.

@rphillips
Created May 18, 2018 20:50
Show Gist options
  • Save rphillips/37da0874b3a44e1db8b2aca6151c1e62 to your computer and use it in GitHub Desktop.
Save rphillips/37da0874b3a44e1db8b2aca6151c1e62 to your computer and use it in GitHub Desktop.

Revisions

  1. rphillips renamed this gist May 18, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rphillips created this gist May 18, 2018.
    40 changes: 40 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #!/usr/bin/env bash

    ## Script to mark nodes for termination on AWS
    ## usage: term-cluster.sh [filter on name] [region (default: us-east-1)]
    ##
    ## Dependencies: jq and awless
    ##

    set -eou pipefail

    die() {
    echo $@
    exit 1
    }

    FILTER=${1:-}
    if [[ $FILTER == "" ]]; then
    die "invalid filter"
    fi

    REGION=${2:-us-east-1}
    declare -A instances=()

    # Query instances
    awsdata=$(awless list instances --filter name="$FILTER" --aws-region=$REGION --format json --no-sync)
    filtered=$(echo $awsdata | jq -r '.[] | "\(.ID) \(.Name) "')
    while read id name; do
    echo "Instance $id - $name"
    instances[$id]=$name
    done <<< "$filtered"

    # Prompt for deletion
    read -p "Terminate these instances? " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
    for key in "${!instances[@]}"; do
    name=${instances[$key]}
    awless create tag resource=$key key=Name value="$name-terminate" --aws-region=$REGION -f --no-sync
    done
    fi