Create and download an AWS ec2 keypair using python boto

一世执手 提交于 2019-12-09 11:54:22

问题


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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/11658578/create-and-download-an-aws-ec2-keypair-using-python-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!