Skip to content

Instantly share code, notes, and snippets.

@z0mbix
Created July 3, 2015 23:22
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 z0mbix/7c35f73e567be2fd597e to your computer and use it in GitHub Desktop.
Save z0mbix/7c35f73e567be2fd597e to your computer and use it in GitHub Desktop.
SSH to an EC2 instance by name
#!/usr/bin/env bash
#
# SSH to an EC2 instance by name
#
# You need the AWS cli (pip install awscli)
#
if [[ -z $1 ]]; then
echo 'You must specify an instance name!'
exit 1
fi
name=$1
instance=$(aws ec2 describe-instances \
--output=text \
--query 'Reservations[*].Instances[*].[PublicIpAddress]' \
--filters "Name=tag:Name,Values=$name")
if [[ -z $instance ]]; then
echo "Instance $name not found!"
exit 1
fi
ssh $instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment