How to configure knife and EC2 to create a new instance from the command line

amazon ec2amazon-web-serviceschefknife

I am playing with Amazon EC2 to create instance. I was reading here: here in the knife documenation for EC2, that I would need to set:

# EC2:
knife[:aws_access_key_id]     = "Your AWS Access Key"
knife[:aws_secret_access_key] = "Your AWS Secret Access Key"

Now, when I try to set the security credentials from the AWS console in my knife.rb, I get a

ERROR: You did not provide a valid 'AWS SSH Key Id' value.

And, when I create a new "keypair" I get some .pem file, which confuses me too.

How do I setup my knife and EC2 to create a new node?

Best Answer

I'm assuming that you are receiving this error while creating a new node and not when you try to set the security credentials in your knife.rb.

To be precise, the error should come up when you initiate:

knife ec2 server create -I <ami-id>

The error

ERROR: You did not provide a valid 'AWS SSH Key Id' value.

is due to a missing SSH Keypair in your knife ec2 server create command. You need to specify an SSH Keypair whenever you launch an Amazon EC2 instance. This SSH public key is added to a newly launched instance to enable a passwordless SSH login. Keypairs can either be created or imported. Keypairs (for US-East region) can be found at EC2 -> Keypairs section of your Amazon AWS console.

Once you pick an SSH Keypair, you could specify it's name in your knife ec2 create command as:

knife ec2 server create -I <ami-id> -S <your-ssh-keypair-name>

Note: This will launch an EC2 instance out of the specified ami-id and with the SSH keypair. Knife will proceed towards knife bootstrap once the instance is launched. To do so, you need to make sure the SSH keypair's private key is added to your ssh-agent session before initiating a knife ec2 server create. You might also want to specify your Chef server URL with --server-url option of knife ec2 server create.