Creating EC2 instances with key pairs in Boto2
<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>

<p>This code (derived from yours) worked for me:</p>

<pre><code>&gt;&gt;&gt; import boto.ec2
&gt;&gt;&gt; conn = boto.ec2.connect_to_region('ap-southeast-2')
&gt;&gt;&gt; key_res = conn.get_all_key_pairs(keynames=['class'])[0]
&gt;&gt;&gt; key_res
KeyPair:class
&gt;&gt;&gt; key_res.name
u'class'
&gt;&gt;&gt; conn.run_instances('ami-ced887ad', key_name=key_res.name, instance_type='t1.micro', security_group_ids=['sg-94cb39f6'])
Reservation:r-0755a9700e3886841
</code></pre>

<p>I then tried your key-creating code:</p>

<pre><code>&gt;&gt;&gt; key_aws = conn.create_key_pair('newkey')
&gt;&gt;&gt; key_aws.save(".")
True
&gt;&gt;&gt; conn.run_instances('ami-ced887ad', key_name=key_aws.name,instance_type='t1.micro',security_group_ids=['sg-93cb39f6'])
Reservation:r-07f831452bf869a14
&gt;&gt;&gt; conn.run_instances('ami-ced887ad', key_name='newkey',instance_type='t1.micro',security_group_ids=['sg-93cb39f6'])
Reservation:r-0476ef041ed26a52f
</code></pre>

<p>It seems to work fine!</p>

python amazon-web-services amazon-ec2 boto