import click


def prepare_instance_authentication(ctx, session, public_key):
    """
    Sends an ephemeral public key to the target instance used to authenticate the SSH session.
    """
    click.echo("sending public key to target instance...")
    client = session.client("ec2-instance-connect")
    return client.send_ssh_public_key(
        InstanceId=ctx.obj["ssh_instance_id"],
        InstanceOSUser=ctx.obj["ssh_instance_user"],
        SSHPublicKey=public_key,
        AvailabilityZone=ctx.obj["ssh_instance_az"],
    )