Skip to content

Instantly share code, notes, and snippets.

@blalor
Created March 13, 2014 13:35
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 blalor/9528597 to your computer and use it in GitHub Desktop.
Save blalor/9528597 to your computer and use it in GitHub Desktop.
ssh to ec2 instances by Name tag

Log into EC2 instances naturally without having to figure out their public hostname ahead of time!

$ ssh ec2-user@web-prod-1.ec2
Last login: Wed Mar 12 22:06:27 2014
[ec2-user@ip-10-158-37-24 ~]$ 
#!/bin/bash
set -e -u
instance_name="$1"
port="$2"
hostname="$( aws ec2 describe-instances --filters Name=tag:Name,Values=${instance_name%.ec2} | jq -r .Reservations[].Instances[0].PublicDnsName )"
exec nc ${hostname} ${port}
Host *.ec2
ProxyCommand ~/bin/ssh-ec2.sh %h %p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment