Create and load an AWS ec2 key pair with python boto

I find it difficult to figure out a way (if possible) to create a new AWS key pair with the Python Boto library, and then load this key pair.

+6
source share
2 answers

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, see https://github.com/garnaat/paws/blob/master/ec2_launch_instance.py .

Does it help? If not, indicate some features of the problems you are facing.

+11
source

The same for Boto 3 :

 ec2 = boto3.resource('ec2') keypair_name = 'my_key' try: return ec2.KeyPair(keypair_name).key_fingerprint except: new_keypair = ec2.create_key_pair(KeyName=keypair_name) with open('./my_key.pem', 'w') as file: file.write(new_keypair.key_material) return new_keypair.key_fingerprint 
0
source

Source: https://habr.com/ru/post/921347/


All Articles