Skip to content

Instantly share code, notes, and snippets.

@rfairburn
Last active October 28, 2019 15:57
Show Gist options
  • Save rfairburn/2d99d78a4f933e29ba5ea43c9d452ea9 to your computer and use it in GitHub Desktop.
Save rfairburn/2d99d78a4f933e29ba5ea43c9d452ea9 to your computer and use it in GitHub Desktop.
aws cli ec2 bash ssh wrapper thingy
#!/bin/bash
REGION=us-east-2
set -e
function get_host() {
local HOSTLIST="${@}"
local HOSTARRAY=( $(awk 'BEGIN { OFS="|" } { print $1,$2 }' <<<"${HOSTLIST}") )
cat -n <<<"${HOSTLIST}" | column -t
read -p "Please enter selection >" HOSTENTRY
IFS="|" read -r HOST IP <<<"${HOSTARRAY[$((${HOSTENTRY}-1))]}"
}
if [ "$1" == "--list" ]; then
QUERY='Reservations[*].Instances[*].[Tags[?Key==`Name`]|[0].Value,PublicIpAddress]'
HOSTLIST=$(aws ec2 describe-instances --region ${REGION} --output text --query ${QUERY})
get_host "${HOSTLIST}"
else
INSTANCE_NAME=${1:?}
FILTERS="Name=tag:Name,Values=${INSTANCE_NAME}"
QUERY='Reservations[*].Instances[*].[PublicIpAddress]'
IP=$(aws ec2 describe-instances --region ${REGION} --filters ${FILTERS} --output text --query ${QUERY})
fi
shift
ssh ${IP:?} $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment