Skip to content

Instantly share code, notes, and snippets.

@kylealwyn
Forked from louiszuckerman/as-terminate
Created August 15, 2017 22:43
Show Gist options
  • Select an option

  • Save kylealwyn/60fdf018fc9f77f72dc5c40c5158297b to your computer and use it in GitHub Desktop.

Select an option

Save kylealwyn/60fdf018fc9f77f72dc5c40c5158297b to your computer and use it in GitHub Desktop.

Revisions

  1. @louiszuckerman louiszuckerman revised this gist Jan 29, 2015. 2 changed files with 54 additions and 0 deletions.
    54 changes: 54 additions & 0 deletions as-terminate
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/bin/bash

    if [ "$1" == "-h" ]; then
    cat <<END
    AWS AutoScaling Termination Helper:
    terminate an instance in an auto scaling group
    Usage: $0 {group-name}
    END
    exit
    fi

    INSTANCES=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $1 --query 'AutoScalingGroups[0].Instances[]' --output text)

    INSTANCEIDS=$(awk '{print $3}' <<<"$INSTANCES")

    COUNT=$(wc -l <<<"$INSTANCES")

    IFS=$'\n'
    c=1
    for i in $INSTANCES; do
    echo $((c++)): $i
    done

    read -p "Which one? " N
    if [[ "$N" == "" || "$N" != [0-9]* || "$N" -gt "$COUNT" ]]; then
    echo ERROR: Invalid selection
    exit
    fi

    read -p "Replace? [Y/n] " R

    if [[ "$R" != [nN] ]]; then
    TERMREPL=REPLACE
    DECREMENT='--no-should-decrement-desired-capacity'
    else
    TERMREPL=TERMINATE
    DECREMENT='--should-decrement-desired-capacity'
    fi

    SELECTEDID=$(head "-$N" <<<"$INSTANCEIDS" | tail -1)
    SELECTEDLINE=$(head "-$N" <<<"$INSTANCES" | tail -1)

    echo About to $TERMREPL instance $N: $SELECTEDLINE
    read -p "Are you sure? [y/N] " S

    if [[ "$S" != [yY] ]]; then
    echo Aborting.
    exit
    else
    echo Proceeding.
    aws autoscaling terminate-instance-in-auto-scaling-group $DECREMENT --instance-id $SELECTEDID
    fi
    File renamed without changes.
  2. @louiszuckerman louiszuckerman revised this gist Jan 29, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -5,20 +5,22 @@ if [ "$1" == "-h" ]; then
    AWS AutoScaling SSH Helper:
    connects to an autoscaling instance by ssh
    Usage: $0 [group-name]
    Usage: $0 {group-name} [index]
    END
    exit
    fi

    INSTANCES=$(as-describe-auto-scaling-groups $1 | grep ^INSTANCE)
    INSTANCES=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $1 --query 'AutoScalingGroups[0].Instances[]' --output text)

    INSTANCEIDS=$(awk '{print $2}' <<<"$INSTANCES")
    INSTANCEIDS=$(awk '{print $3}' <<<"$INSTANCES")

    COUNT=$(wc -l <<<"$INSTANCES")

    if [ $COUNT == 1 ]; then
    N="1"
    elif [ "$2" != "" ]; then
    N=$2
    else
    IFS=$'\n'
    c=1
    @@ -34,4 +36,4 @@ fi
    SELECTEDID=$(head "-$N" <<<"$INSTANCEIDS" | tail -1)
    SELECTEDLINE=$(head "-$N" <<<"$INSTANCES" | tail -1)
    echo Selected $N: $SELECTEDLINE
    ssh $(euca-describe-instances $SELECTEDID | grep ^INSTANCE | awk '{print $14}')
    ssh $(aws ec2 describe-instances --instance-ids $SELECTEDID --query "Reservations[0].Instances[0].PublicIpAddress" --output text)
  3. @louiszuckerman louiszuckerman revised this gist Jan 8, 2013. No changes.
  4. @louiszuckerman louiszuckerman created this gist Jan 8, 2013.
    37 changes: 37 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash

    if [ "$1" == "-h" ]; then
    cat <<END
    AWS AutoScaling SSH Helper:
    connects to an autoscaling instance by ssh
    Usage: $0 [group-name]
    END
    exit
    fi

    INSTANCES=$(as-describe-auto-scaling-groups $1 | grep ^INSTANCE)

    INSTANCEIDS=$(awk '{print $2}' <<<"$INSTANCES")

    COUNT=$(wc -l <<<"$INSTANCES")

    if [ $COUNT == 1 ]; then
    N="1"
    else
    IFS=$'\n'
    c=1
    for i in $INSTANCES; do
    echo $((c++)): $i
    done
    read -p "Which one? [1] " N
    if [[ "$N" == "" || "$N" != [0-9]* || "$N" -gt "$COUNT" ]]; then
    N="1"
    fi
    fi

    SELECTEDID=$(head "-$N" <<<"$INSTANCEIDS" | tail -1)
    SELECTEDLINE=$(head "-$N" <<<"$INSTANCES" | tail -1)
    echo Selected $N: $SELECTEDLINE
    ssh $(euca-describe-instances $SELECTEDID | grep ^INSTANCE | awk '{print $14}')