Generating a Keypair for Amazon EC2
Before you do anything on EC2, you need a keypair plain and simple. There are two easy ways to generate a key: one is using Elasticfox (my preferred method), and the other is using Amazon’s EC2-API-tools. If you are reading this tutorial, I’m assuming you’re new to working with EC2. A very brief run down of how to get the EC2-API-tools working:
- Goto http://aws.amazon.com, sign up for EC2
- Under “Your Account” click “Access Identifiers” (you will likely come back here often)
- Generate a certificate (you should get two *.pem files cert-somelonghash.pem and pk-somelonghash.pem
- Make a directory in your home folder, call it something like “ec2” or “.ec2” or “aws”. Put the files here.
- Download the tools (link above)
- Install and configure the tools (Amazon has a good write-up on this)
Now that all that is taken care of, we can get started (for reals this time).
ec2-add-keypair cloud-carpenters<br>
This will generate the key signature (which will appear in ec2-describe-keypairs and in the “KeyPairs” tab of Elasticfox) as well as the private key itself. The generated key will look like
-----BEGIN RSA PRIVATE KEY----- A Whole mess of stuff -----END RSA PRIVATE KEY-----
You’ll want to copy/paste the contents of the key (including the BEGIN/END lines) into a blank file. It would actually be easier to just cat the output of ec2-add-keypair like
ec2-add-keypair cloud-carpenters > id-cloud-carpentersand then remove the first line of the file. This way you don’t have to worry about your text editor doing funny things with wrapping and newlines.
In Elasticfox, click the green key icon and give the key a name.

Once generated, Elasticfox will prompt you to download your freshly generated key. Regardless of the method you chose, move the key into a safe directory (like ~/.ssh) and change the permissions to 400 (read-only by you).
To use this key, you need to spawn instances with it. This is super simple in Elasticfox, and not too complicated with the command line tools. With the command line tools, you need to add “-k” to the argument list with the name of the key you just created
ec2-run-instance ami-0123abcd -k cloud-carpenters
Or in Elasticfox

Once the instance is available, you can ssh into using the key (Windows users, check out our Putty tutorial)
ssh root@ec2-72-44-61-6.compute-1.amazonaws.com -i ~/.ssh/id-cloud-carpenters
And there you have it. Oh, and don’t use cloud-carpenters as your keyname - that was just for the tutorial. Use something like “home” or “yourname”.
New Comment
Commenting is not available in this weblog entry.