I'm having difficulty figuring out a way (if possible) to create a new AWS keypair with the Python Boto library and then download that keypair.
Asked
Active
Viewed 5,643 times
8
-
Instead of having Amazon create the keypair, I recommend creating the ssh key yourself and uploading the public key to EC2. Here's an article I wrote about that: http://alestic.com/2010/10/ec2-ssh-keys You should be able to use boto to perform the "ec2-import-keypair" API call. – Eric Hammond Jul 30 '12 at 23:02
2 Answers
11
The Key object returned by the create_keypair method in boto has a "save" method. So, basically you can do something like this:
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> key = ec2.create_key_pair('mynewkey')
>>> key.save('/path/to/keypair/dir')
If you want a more detailed example, check out https://github.com/garnaat/paws/blob/master/ec2_launch_instance.py.
Does that help? If not, provide some specifics about the problems you are encountering.

garnaat
- 44,310
- 7
- 123
- 103
3
Same for Boto3:
ec2 = boto3.resource('ec2')
keypair_name = 'my_key'
new_keypair = ec2.create_key_pair(KeyName=keypair_name)
with open('./my_key.pem', 'w') as file:
file.write(new_keypair.key_material)
print(new_keypair.key_fingerprint)

Michael A.
- 1,071
- 12
- 21