• Archive
  • RSS
banner

Simplifying EC2 SSH Connections

Here at Instagram, we run our infrastructure on Amazon Web Services, running instances on their Elastic Compute Cloud (EC2). Since we’re often spinning up new machines and changing details of our infrastructure, there’s an ever-growing list of machines that we SSH into.

To authenticate with our instances, we use public key authentication (the recommended way of doing SSH log-ins to EC2 machines), but we need to figure out what host we’re connecting to first. A common way of connecting to an EC2 instance is via it’s public hostname (ec2-x-x-x-x.compute-1.amazonaws.com). However, managing a list of these hostnames once there are several of them is tedious. Because of this, we wrote EC2-SSH, a set of Python scripts that help easily connect to EC2 instances, and that we’re open sourcing for the community’s benefit.

EC2-SSH takes advantage of the EC2 instance tagging. If you’re using the web console, when launching a new instance, Amazon prompts you to provide an optional value for the (pre-filled) “Name” tag. Tags can also be edited using the available EC2 command line tools.

Assuming you’ve already tagged all of your instances with names, using EC2-SSH is as easy as using regular SSH with hostnames— because, behind the scenes, that’s all it’s really doing. Better illustrated with an example: Let’s assume you have an instance tagged with the name “nginx3”; using EC2-SSH you could connect to the instance by typing `ec2-ssh nginx3` into your terminal.

EC2-SSH first calls the Amazon EC2 web service, resolving the tag name (in this case “nginx3”) to the public DNS address. It then substitutes out the tag name with the hostname and sends it, along with any other arguments and parameters, to `/usr/bin/ssh`.

The `ec2-ssh` script is small shell script that calls another Python script, `ec2-host`, that eventually calls `/usr/bin/ssh`. Let’s detail out the process in depth:

The Python script `ec2-host` is distributed in the EC2-SSH Python package and can be used unaccompanied— you might find it rather useful, in fact. Let’s take a look at it’s usage output:

% ec2-host --help

Usage: ec2-host [-k KEY] [-s SECRET] [NAME]
Prints server host name.

  --help                  display this help and exit
  -k, --aws-key KEY       EC2 Key, defaults to ENV[AWS_ACCESS_KEY_ID]
-s, --aws-secret SECRET EC2 Secret, defaults to ENV[AWS_SECRET_ACCESS_KEY]

By default, with no arguments, `ec2-host` host will return a list of all running EC2 instances and their associated public host names. I often use `ec2-host` this way, combined with grep I use it to filter out and identify a specific instance, or set of instances. Here’s an example:

% ec2-host | grep django
django1    ec2-x-x-x-x.amazonaws.com
django2    ec2-x-x-x-x.amazonaws.com
....

When passing the value of an instance’s “Name” tag as an argument, ec2-host will return the associated public hostname. This is exactly what the `ec2-ssh` shell script does. Here’s an example:

% ec2-host nginx2
ec2-x-x-x-x.compute-1.amazonaws.com

You may be asking how `ec2-host` has access to enumerate over your running EC2 instances, that’s a valid question. Before you start using `ec2-ssh` or `ec2-host` you have to provide your AWS key and secret. You can pass them via command line arguments to `ec2-host` like this:

% ec2-host --aws-key  AKJASKSA1234JDSJ8123 --aws-secret B3JDJRYQ1234QWRHFJ1234AJJDAH1kjd1234

To save time, you can also set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

Putting it all together, if you issue the following command, you’ll be signed in to the instance in your system with the specified name:

% ec2-ssh nginx3

Any arguments after the instance name will be passed into ssh:

% ec2-ssh nginx5 date  
Thu Oct 13 17:25:22 UTC 2011

We’ve made these tools available via PyPy, so you can issue an `easy_install ec2-ssh` or a `pip install ec2-ssh` to install the tools. If you’d like to contribute, you can also fork the code on GitHub, or discuss this post on Hacker News . And if you’re interested in helping us scale our systems, we’d love to hear from you.

Shayne Sweeney, Mobile & Server Engineer

  • 7 months ago
  • 112
  • Permalink
  • Share
    Tweet

112 Notes/ Hide

  1. minerva-wealth-protect-1234 reblogged this from instagram-engineering
  2. strictlandiirh26332 reblogged this from instagram-engineering
  3. lincoln-nail-technician reblogged this from instagram-engineering
  4. symetry-genesis liked this
  5. ionelapitigoi22 reblogged this from instagram-engineering
  6. freesamplesuk reblogged this from instagram-engineering
  7. fising-lurs-for-bass liked this
  8. agence-de-publicite-luxembourg liked this
  9. agence-de-publicite-luxembourg reblogged this from instagram-engineering
  10. fastvision-web-hosting liked this
  11. fastvision-web-hosting reblogged this from instagram-engineering
  12. blogbourse reblogged this from instagram-engineering
  13. membership-to-go-4 reblogged this from instagram-engineering
  14. printers-reviews reblogged this from instagram-engineering
  15. quitar-manchas-de-las-axilas reblogged this from instagram-engineering
  16. quitar-manchas-de-las-axilas liked this
  17. anime-hentai liked this
  18. live-sport liked this
  19. play-free-online-games-now reblogged this from instagram-engineering
  20. breathalyzer-reviews reblogged this from instagram-engineering
  21. buy-poppers reblogged this from instagram-engineering
  22. cougar--dating reblogged this from instagram-engineering
  23. butifarra reblogged this from instagram-engineering
  24. beatrice-smith reblogged this from instagram-engineering
  25. unique-hoodia-2012 reblogged this from instagram-engineering
  26. qgifs liked this
  27. krill-oil-for-dogs reblogged this from instagram-engineering
  28. dry-cleaning-chicago reblogged this from instagram-engineering
  29. free-registry-cleaner reblogged this from instagram-engineering
  30. stop-snoring-pillow reblogged this from instagram-engineering
  31. foreclosure-listings reblogged this from instagram-engineering
  32. outlook-pst--repair reblogged this from instagram-engineering
  33. unlock-iphone-3g reblogged this from instagram-engineering
  34. speed-up-my-pc reblogged this from instagram-engineering
  35. gregorynicholas liked this
  36. business-news-time liked this
  37. brazzers-passwords liked this
  38. stackable-washer-dryer liked this
  39. instagram-engineering posted this
← Previous • Next →
We're sharing the tools + techniques we've learned in bringing photo-sharing to millions of people

Pages

  • Instagram Blog

Instagram on the web

  • @instagram on Twitter
  • Facebook Profile
  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr