Wednesday 3 December 2014

Access or connect amazon ec2 using terminal

I’m setting up some free Amazon EC2 instances so I can run my Twitter data collectors without violating the terms of my Dreamhost hosting agreement or worrying about losing power on my office machine (happened just last week). Despite the many Google results for “getting started with ec2 on a mac”, the process is not that complicated.

Instructions

First, you need to generate an SSH key pair. The AWS Management Console makes this easy: Choose “Key Pairs” from the left menu bar under EC2. Follow the instructions, and download your *.pem somewhere you’ll remember.

Then, start your EC2 instance with that key pair and associate your elastic IP with that instance.

Once you have an EC2 instance up and running with your designated key pair, you should be able to SSH in just like you do to other servers:
$sudo chmod 0700 ec2-keypair.pem
$ssh -i ec2-keypair.pem ec2-user@ec2-elastic-ip

(Or, use ssh-add ec2-keypair.pem before ssh so you can drop the -i option)

Common problems

Problem: I get Permission denied (publickey).
Solutions:



Make sure you’re using the right public key (check your EC2 Management Console. The Key Pair Name should match the *.pem you’re using.
Use the full path to the *.pem file for ec2-keypair.
Make sure you’re using the right username, should be ec2-user

Notes

Many forums recommend Elasticfox Firefox extension for managing your AWS instances. That’s overkill if you just want to SSH in and get something started. Clicking the “Connect to Instance” button in Elasticfox just opens terminal with the SSH command above anyway. So, if you’re a happy Chrome user like I am, you can continue to avoid Firefox and connect happily on your own via the command line.

No comments:

Post a Comment