Created
February 1, 2018 22:00
-
-
Save bpholt/7fa1fb3ebb18b77865bf0253c8e53ca9 to your computer and use it in GitHub Desktop.
ssh via EC2 Instance Name tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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