Create and download an AWS ec2 keypair using python boto

Viewed 5365

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.

2 Answers

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)
Related