title: Creating EC2 instances with key pairs in Boto2
body: <p>I have the following code using boto.ec2 to connect to Amazon EC2 from python, but I'm struggling to deal with the .pem files. If I pass None to the run_instances call as the key name, I can create instances without any problem. However, if I pass any key name (whether or not I create it using the console, or manually as below), I systematically get the following error when I try to run an instance </p>
  
  <pre><code>EC2ResponseError: 400 Bad Request
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Response&gt;&lt;Errors&gt;&lt;Error&gt;&lt;Code&gt;InvalidKeyPair.NotFound&lt;/Code&gt;&lt;Message&gt;The key pair 'newkey.pem' does not exist&lt;/Message&gt;&lt;/Error&gt;&lt;/Errors&gt;&lt;RequestID&gt;e4da5b1e-a8ec-42fb-b3ce-20aa883a0615&lt;/RequestID&gt;&lt;/Response&gt;
  </code></pre>
  
  <p>When I check on the console for the appropriate region, the key is indeed created (it is also created in my home directory, but I still get the key does not exist error)</p>
  
  <p>Any ideas?</p>
  
  <p>Below is my current Python test code</p>
  
  <pre><code> try:
      key_res = conn.get_all_key_pairs(keynames=[key])[0]
      print key_res
      print "Key pair found"
     except boto.exception.EC2ResponseError, e:
      print e
      if e.code == 'InvalidKeyPair.NotFound':
       print 'Creating keypair: %s' % key
       # Create an SSH key to use when logging into instances.
       key_aws = conn.create_key_pair(key)
       # AWS will store the public key but the private key is
       # generated and returned and needs to be stored locally.
       # The save method will also chmod the file to protect
       # your private key.
       key_aws.save(".")
      else:
        raise
      print "Creating instances"
      try: 
        conn.run_instances(ami[c.region.name], key_name=key,instance_type=instance,security_groups=[security_group])
      except Exception as e:
                  print e
                  print "Failed to create instance in " + c.region.name
  </code></pre>