def get_latest_instance()

in eksupdate/src/boto_aws.py [0:0]


def get_latest_instance(asg_name, add_time,regionName):
    """Retrieve the most recently launched/launching instance. Note that this is not necessarily the same one that was launched by `add_node()`, but it's the best I could think of"""
    asg_client = boto3.client("autoscaling",region_name=regionName) 
    ec2_client = boto3.client("ec2",region_name=regionName)
    instances = []

    response = asg_client.describe_auto_scaling_groups(
        AutoScalingGroupNames=[
            asg_name
        ]
        )
    time.sleep(20)
    instance_ids = [instance["InstanceId"]
                    for instance in response["AutoScalingGroups"][0]["Instances"]]

    response = ec2_client.describe_instances(InstanceIds=instance_ids)
    for reservation in response["Reservations"]:
        for instance in reservation["Instances"]:
            instances.append(instance)
    # print(instances)
    instance_launch_times = [
        {x["PrivateDnsName"]: x["LaunchTime"]} for x in instances]

    instances_valid = []
    instances_valid = [instance for instance in instances if instance["State"]["Name"] in [
        "pending", "running"] and instance["LaunchTime"] > add_time]

    latest_instance = ""
    try:
        time.sleep(10)
        latest_instance = sorted(
            instances_valid, key=lambda instance: instance["LaunchTime"])[-1]
    except Exception as e:
        # logs_pusher(e,"error")
        raise Exception(e)

    return latest_instance.get('InstanceId')