|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Controlling EC2 from the Console |
| 4 | +--- |
| 5 | + |
| 6 | +# Controlling EC2 from the Console |
| 7 | + |
| 8 | +[Amazon Web Services'](http://aws.amazon.com/) [Elastic Compute Cloud](http://aws.amazon.com/ec2/) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers. |
| 9 | + |
| 10 | +You can control your EC2 instances through the [AWS Management Console](https://console.aws.amazon.com/ec2/), but clicking around can be a chore. This tutorial will help you setup your machine so you can spawn new instances, query existing instances, and do general maintainence without leaving the comfort of a terminal. |
| 11 | + |
| 12 | +<div |
| 13 | +markdown="1" |
| 14 | +class="tutorial" |
| 15 | +data-author-github="Whitespace" |
| 16 | +data-license="http://creativecommons.org/licenses/by/3.0/" |
| 17 | +data-facets='{"Operating System": "OS X", "Package Management": "Homebrew", "Shell": "bash"}'> |
| 18 | + |
| 19 | +## Assumptions |
| 20 | +We assume you've [signed up for EC2](https://aws-portal.amazon.com/gp/aws/developer/registration). We also assume your machine has a public key setup (commonly `~/.ssh/id_rsa` and `~/id_rsa.pub`). |
| 21 | + |
| 22 | +## Download Credential Files |
| 23 | +First, you need to download some credential files to your machine. [Download your X.509 certificates](https://portal.aws.amazon.com/gp/aws/securityCredentials) and then put them into your `~/.ec2` directory: |
| 24 | + |
| 25 | +{% highlight sh %} |
| 26 | +mkdir ~/.ec2 |
| 27 | +cp ~/Downloads/[cert,pk]-*.pem ~/.ec2 |
| 28 | +{% endhighlight %} |
| 29 | + |
| 30 | +## Install the EC2 Command Line Tools |
| 31 | +Now let's install the two packages we need: |
| 32 | + |
| 33 | +{% highlight sh %} |
| 34 | +brew install ec2-ami-tools |
| 35 | +brew install ec2-api-tools |
| 36 | +{% endhighlight %} |
| 37 | + |
| 38 | +Copy the following to your `~/.bash_profile`: |
| 39 | + |
| 40 | +{% highlight sh %} |
| 41 | +export JAVA_HOME="$(/usr/libexec/java_home)" |
| 42 | +export EC2_PRIVATE_KEY="$(/bin/ls "$HOME"/.ec2/pk-*.pem | /usr/bin/head -1)" |
| 43 | +export EC2_CERT="$(/bin/ls "$HOME"/.ec2/cert-*.pem | /usr/bin/head -1)" |
| 44 | +export EC2_HOME="/usr/local/Library/LinkedKegs/ec2-api-tools/jars" |
| 45 | +export AWS_ACCESS_KEY_ID="..." |
| 46 | +export AWS_SECRET_ACCESS_KEY"=..." |
| 47 | +{% endhighlight %} |
| 48 | + |
| 49 | +Where `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` are available from the "Access Keys" tab on the [https://portal.aws.amazon.com/gp/aws/securityCredentials](Amazon Security Credentials page). |
| 50 | + |
| 51 | +And we |
| 52 | + |
| 53 | +## Uploading your Access Keys |
| 54 | +Some people use the public/private keys that Amazon gives them, but I find it easier to use the same public key on my laptop that I already use for things like Github and servers at work. To upload, you can use [this helpful script from Eric Hammond](http://alestic.com/2010/10/ec2-ssh-keys) (slightly modified): |
| 55 | + |
| 56 | +{% highlight sh %} |
| 57 | +keypair=macbook # Choose something that makes sense, like your computer name |
| 58 | +publickeyfile=$HOME/.ssh/id_rsa.pub # Point this to the public key you want to use |
| 59 | +regions=$(ec2-describe-regions | cut -f2) |
| 60 | + |
| 61 | +for region in $regions; do |
| 62 | + echo $region |
| 63 | + ec2-import-keypair --region $region --public-key-file $publickeyfile $keypair |
| 64 | +done |
| 65 | +{% endhighlight %} |
| 66 | + |
| 67 | +## Test it All Works |
| 68 | +Once we've done all that, we can test it by doing: |
| 69 | + |
| 70 | +{% highlight sh %} |
| 71 | +ec2-describe-instances |
| 72 | +{% endhighlight %} |
| 73 | + |
| 74 | +If you don't see an error, you're all set! |
| 75 | + |
| 76 | +## References |
| 77 | +These links were helpful in getting my machine setup and writing this tutorial: |
| 78 | + |
| 79 | +* [Starting Amazon EC2 with Mac OS X](http://www.robertsosinski.com/2008/01/26/starting-amazon-ec2-with-mac-os-x/) |
| 80 | +* [Uploading Personal ssh Keys to Amazon EC2](http://alestic.com/2010/10/ec2-ssh-keys) |
| 81 | +</div> |
0 commit comments