Setting up knife ec2

I keep getting Fog::Compute::AWS::NotFound: The key pair … does not exist error

I keep getting Fog::Compute::AWS::NotFound: The key pair ... does not exist error!

How to solve this problem isn’t explained well enough anywhere.

Install knife-ec2 first:

sudo gem install knife-ec2

Then I added this to my knife.rb:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# key name, as defined in EC2 Key Pairs,
# this value has to be exactly the same as the one in the management console:
knife[:aws_ssh_key_id]        = "my-ec2-key"

# AWS ID/SECRET:
knife[:aws_access_key_id]     = "..."
knife[:aws_secret_access_key] = "..."

# Some defaults
knife[:region]                = "eu-west-1"
knife[:availability_zone]     = "eu-west-1a"
knife[:ssh_user]              = "ubuntu"
knife[:flavor]                = "m1.medium"
# Ubuntu 12.04 LTS 64bit:
knife[:image]                 = "ami-f2191786"
knife[:use_sudo]              = "true"

As I was trying to create an instance with this command:

knife ec2 server create -G "default" -r 'recipe[monit]'

I kept getting following error:

Fog::Compute::AWS::NotFound: The key pair 'my-ec2-key' does not exist

It turned out, 2 hours later, in order to fix this I had to run:

ssh-add ~/.ssh/my-ec2-key.pem

It worked.