Skip to content

Instantly share code, notes, and snippets.

@mak3r
Last active July 9, 2019 04:22
Show Gist options
  • Save mak3r/158caf155341a2d27373baffd383f6a7 to your computer and use it in GitHub Desktop.
Save mak3r/158caf155341a2d27373baffd383f6a7 to your computer and use it in GitHub Desktop.
Find a list of aws instances based on their tag "Name"

Access aws instances easily even after dns name changes

Restarts of aws ec2 instances can cause ips and dns names to change if you don't pay for static IPs

This set of scripts enables quick ssh access to a group of similarly named machines

  1. run generate-instance-list.sh to get a list of aws instance ids
  2. run ssh-connect.sh against the generated list to access any of the instances

TODO

Currently it is not possible to differentiate the machines easily - enable access by more human readable names in the generated instance list.

#!/bin/bash
NAME="abc"
VERBOSE=0
while getopts "n:v" opt; do
case $opt in
n)
#echo "handling name -$OPTARG"
NAME=$OPTARG
;;
v)
VERBOSE=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [ "$VERBOSE" -ne 0 ]; then echo $NAME; fi
#ids=$(aws ec2 describe-tags --filters "Name=tag:Name,Values=$NAME" "Name=resource-type,Values=instance" | jq ."Tags"[]."ResourceId" | aws ec2 describe-instances --filters "Name=instance-state-code,Values=16" --instance-ids -- | jq '."Reservations"[]."Instances"[]."InstanceId"')
#set -x
FIND_BY_NAME=(/usr/local/bin/aws "ec2" "describe-tags" "--filters" "Name=tag:Name,Values=$NAME*" "Name=resource-type,Values=instance")
NAMED_RESOURCES=`${FIND_BY_NAME[@]}`
if [ "$VERBOSE" -ne 0 ]; then echo Named Resources: $NAMED_RESOURCES; fi
TAG_LIST=(/usr/local/bin/jq "-r" ".\"Tags\"" )
TAG_LENGTH=(/usr/local/bin/jq "length")
#| length>0 then . else \"empty\" end'")
if [ "$VERBOSE" -ne 0 ]; then echo is empty: "${TAG_LIST[@]}"; fi
LIST_LENGTH=`${FIND_BY_NAME[@]} | ${TAG_LIST[@]} | ${TAG_LENGTH[@]}`
if [ "$VERBOSE" -ne 0 ]; then echo Tags list length: "$LIST_LENGTH"; fi
if [ "$LIST_LENGTH" -gt 0 ]; then
PULL_RESOURCE_IDS=(/usr/local/bin/jq "-r" ".\"Tags\"[].\"ResourceId\"")
BY_RESOURCE_ID=`${FIND_BY_NAME[@]} | ${PULL_RESOURCE_IDS[@]} `
if [ "$VERBOSE" -ne 0 ]; then echo BYID $BY_RESOURCE_ID BYID; fi
#instance-state-code=16 is RUNNING
DESCRIBE_RUNNING=(/usr/local/bin/aws "ec2" "describe-instances" "--filters" "Name=instance-state-code,Values=16" "--instance-ids" "$BY_RESOURCE_ID")
if [ "$VERBOSE" -ne 0 ]; then echo RUNNING ${DESCRIBE_RUNNING[@]} RUNNING ; fi
ONLY_RESOURCE_IDS=(/usr/local/bin/jq '."Reservations"[]."Instances"[]."InstanceId"')
IDS=`${DESCRIBE_RUNNING[@]} | ${ONLY_RESOURCE_IDS[@]} `
if [ "$VERBOSE" -ne 0 ]; then echo $IDS; fi
fi
printf '{ \"'"mak3r-k8s"'\": { "instances": ['
if [ "$LIST_LENGTH" -gt 0 ]; then
j=0
for i in $IDS; do
if [ $j -ne 0 ]; then
printf ,
fi
printf $i;
let j=j+1
done
fi
echo ']}}'
#!/bin/bash
ID=0
NO_EXEC=0
KEY=""
DEBUG=0
STRICT=""
while getopts "k:i:Nvs" opt; do
case $opt in
i)
ID=$OPTARG
;;
k)
KEY=$OPTARG
;;
s)
#dont do strict host key checking in ssh
STRICT="-oStrictHostKeyChecking=no"
;;
N)
NO_EXEC=1
;;
v)
DEBUG=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [ "$DEBUG" -eq 1 ]; then
set -vx;
fi
GET_INSTANCE_ID=(/usr/local/bin/jq "-r" ".\"mak3r-k8s\".\"instances\"[$ID]" "instance-ids.json")
INSTANCE_ID=`${GET_INSTANCE_ID[@]}`
DESCRIBE_INSTANCE=(/usr/local/bin/aws "ec2" "describe-instances" "--instance-ids" "$INSTANCE_ID")
PARSE_PUBLIC_DNS=(/usr/local/bin/jq ".\"Reservations\"[0].\"Instances\"[0].\"PublicDnsName\"" "--")
PUBLIC_DNS=`${DESCRIBE_INSTANCE[@]} | ${PARSE_PUBLIC_DNS[@]}`
SSH_CMD=(/usr/bin/ssh "-i" "$KEY" "$STRICT" "ubuntu@$PUBLIC_DNS")
echo ${SSH_CMD[@]}
# Only execute the command if the no exec flag has not been raised.
if [ $NO_EXEC -eq 0 ]
then
echo "Connecting to $PUBLIC_DNS"
eval ${SSH_CMD[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment