Skip to content Skip to sidebar Skip to footer

Ssh Key Of Newly Created Ec2 Instance Using Boto

I am using boto to connect to EC2 and launch an instance. After creating the instance, I need to ssh to it. I need the public ssh key of the server to add that to my known hosts fi

Solution 1:

# Check to see if specified keypair already exists.
# If we get an InvalidKeyPair.NotFound error back from EC2,
# it means that it doesn't exist and we need to create it.
try:
    key = ec2.get_all_key_pairs(keynames=[key_name])[0]
except ec2.ResponseError, e:
    if e.code == 'InvalidKeyPair.NotFound':
        print 'Creating keypair: %s' % key_name
        # Create an SSH key to use when logging into instances.
        key = ec2.create_key_pair(key_name)

        # AWS will store the public key but the private key is
        # generated and returned and needs to be stored locally.
        # The save method will also chmod the file to protect
        # your private key.
        key.save(key_dir)
    else:
        raise

Post a Comment for "Ssh Key Of Newly Created Ec2 Instance Using Boto"