简体   繁体   中英

Boto3: Difference between (EC2)KeyPair and KeyPairInfo

Can anyone point me what is the purpose of Boto3 (resource-level) KeyPair and KeypairInfo objects? Documentation is yet again vague..

import boto3
ec2 = boto3.resource('ec2')
key_pair = ec2.KeyPair('name')
key_pair = ec2.KeyPairInfo('name')

Importantly, KeyPairInfo does not work at all: AttributeError: 'ec2.ServiceResource' object has no attribute 'KeyPairInfo'

Additionally:

  • Both cannot create a new keypair, then why do we have them at all?
  • print key_pair_info.key_material
    AttributeError: 'ec2.KeyPairInfo' object has no attribute 'key_material' . This is because key_material is given to user once while creation.
  • Just for info, I mostly work with botocore(client)

    ec2 = boto3.resource('ec2')
    ec2.KeyPair('name')  # Get the key fingerprint AND the private key
    ec2.KeyPairInfo('name') # Get the key fingerprint ONLY
    
    
    ec2 = boto3.client('ec2')
    mykeypair = ec2.create_key_pair(KeyName='name') # Create a new keypair
    print mykeypair['KeyMaterial']
    

    You can't get the KeyPairInfo directly, but you can get it using this:

    key_pairs = EC2_RESOURCE.key_pairs.filter(
        KeyNames=[
            'my-ssh-key-pair',
        ],
    )
    
    for key in key_pairs:
        print(f'SSH key "{key.key_name}" fingerprint: {key.key_fingerprint}')
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM