Skip to content

Instantly share code, notes, and snippets.

@keturn
Last active February 9, 2016 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keturn/61f76adaaedf4d0eb0b7 to your computer and use it in GitHub Desktop.
Save keturn/61f76adaaedf4d0eb0b7 to your computer and use it in GitHub Desktop.
ssh to an AWS EC2 host by its group name.
#!/bin/bash
# requires AWS CLI: https://aws.amazon.com/cli/
# and jq: https://stedolan.github.io/jq/
set -e
EC2_NAME="$1"
if [ -z "${EC2_NAME}" ] ; then
echo Usage: "$0" '<ec2-name> [ssh args ...]'
fi
shift
EC2_HOSTNAME=$(aws --output json ec2 describe-network-interfaces --filter "Name=group-name,Values=${EC2_NAME}" |
jq --raw-output --exit-status '.NetworkInterfaces[0].Association.PublicDnsName')
exec ssh "ec2-${EC2_NAME}" -o HostName="${EC2_HOSTNAME}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment