Skip to content

Instantly share code, notes, and snippets.

@bpholt
Created February 1, 2018 22:00
Show Gist options
  • Save bpholt/7fa1fb3ebb18b77865bf0253c8e53ca9 to your computer and use it in GitHub Desktop.
Save bpholt/7fa1fb3ebb18b77865bf0253c8e53ca9 to your computer and use it in GitHub Desktop.
ssh via EC2 Instance Name tag
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
function find_ip () {
aws --region us-west-2 \
ec2 describe-instances --filters \
"Name=tag:Name,Values=${1}" \
"Name=instance-state-name,Values=running" | \
jq -rc '.Reservations | map(.Instances) | map(map(.PrivateIpAddress)) | flatten | flatten | .[0]'
}
JUMP_BOX_IP=$(find_ip ${1})
if [ "${JUMP_BOX_IP}" == "null" ]; then
echo "No instance by the name \"${1}\" exists. Are you using the right account?" > /dev/stderr
exit 1
fi
shift
ssh -t $JUMP_BOX_IP "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment