• Home
  • Flailing Wildly
  • Résumé
  • Design
  • Code
  • Wishlist
  • About Me

Flailing Wildly
Too much straw, not enough camel.

Easily SSH into Amazon EC2 instances using the Name tag

by Ryan Parman • January 29, 2014 • Cloud Computing, Code, Security • 1 comments

It’s been a while since I’ve written about code, so I thought I’d post this little AWS-related tip for OS X and Linux users.

If you have the Unified AWS CLI Tools configured, you can add these functions to your Bash profile (typically either ~/.bash_profile or ~/.profile) to enable you to SSH into an instance by “Name” tag, or simply lookup the IP address or DNS hostname.

$ hostname_from_instance <instance-name>

$ ip_from_instance <instance-name>

$ ssh-aws <instance-name>

Bash code

function hostname_from_instance() {
    echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"')
}

function ip_from_instance() {
    echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicIpAddress' | tr -d '"')
}

function ssh-aws() {
    ssh -i ~/.ssh/your-keypair.pem ec2-user@$(ip_from_instance "$1")
}

Notes

  • This assumes that every instance you have has a unique “Name” tag, and will return the IP address or public DNS hostname of that instance (for use with SSH access). If multiple instances share the same “Name” tag, it will simply use the first “Name” match.

  • If you’re running instances inside a (private) VPC, you should expect to lookup the public Elastic IP address for the instance.

  • If you’re running instances inside a (public, classic) EC2, you should expect to lookup the public DNS hostname (unless you’ve configured an Elastic IP — in which case, go nuts).

  • In the case where you’re running instances in the private subnet of a VPC, and SSH access to those instances is only possible from a bastion host in the public subnet, this is not the solution for you.

Feel free to tweak/adjust as necessary.


References

  • Unified AWS CLI Tools
  • JMESpath
  • EC2 API Reference: DescribeInstances
Ryan Parman

Ryan Parman is an entrepreneur, open source evangelist and passionate usability advocate currently living in Seattle. He is the founder and visionary behind SimplePie and CloudFusion, co-founder of WarpShare, member of the RSS Advisory Board, and creator of the AWS SDK for PHP. Ryan's aptly-named blog, Flailing Wildly, is where he writes about ideas longer than 140 characters.

« Games of the Year, 2013
If your website supports passwords, please stop sucking at it »

Discussion

UriAgassi

July 8, 2014

Hey Ryan, I’ve had a similar problem, and I created an open source project which addresses it – check it out – http://sash.agassi.co.il

 

Have your say

Login with your favorite account to leave a comment!

Blog search

Archives
  • 2016 (1)
  • 2015 (3)
  • 2014 (6)
  • 2013 (15)
  • 2012 (16)
  • 2011 (27)
  • 2010 (9)
  • 2009 (6)
  • 2008 (12)
  • 2007 (8)
  • 2006 (18)
  • 2005 (57)
  • 2004 (104)
  • 2003 (103)
Categories
  • Apple (62)
  • Browsers (56)
  • Cloud Computing (5)
  • Clueless Recruiters (9)
  • Code (60)
  • Community (2)
  • Creating Websites (32)
  • Culture (8)
  • Design (7)
  • Digital Media (9)
  • Family Life (12)
  • Just for Fun (27)
  • Law (2)
  • Life Lessons (2)
  • Music (5)
  • Notable Quotes (1)
  • Passwords (4)
  • Personal (41)
  • Political (17)
  • Projects (49)
  • Security (6)
  • Software (69)
  • Syndication (28)
  • Technology (92)
  • The Hiring Process (1)
  • Tutorials (9)
  • TV and Movies (17)
  • Video Games (6)
  • Website (62)
  • Work and Business (8)
  • Writing (4)
Socially-aware

Twitter • Facebook • YouTube • Yelp! • Flickr • Instagram • Zerply • LinkedIn • Last.fm • Spotify • Rdio • Pinboard • gdgt

Claim to fame
  • Amazon Web Services
  • WarpShare
  • CloudFusion
  • SimplePie
Legal mumbo-jumbo

All ideas, opinions and comments I post are my own and are in no way affiliated with anybody I work with. If you quote and/or reprint something I've written or said, please direct folks back to this site as a form of attribution. I promise I'll do the same for you. Unless otherwise noted, all content on this site is copyright © 1979–2011 Ryan Parman.

Powered by Rocket Sauce. A Ryan Parman production.