a method to wait until AWS instance reports an OK status

        :param instance_id: string of instance id on AWS
        :param wait: [optional] boolean to wait for instance while initializing
        :return: True

def check_instance_status(self, instance_id, wait=True):

        '''
            a method to wait until AWS instance reports an OK status

        :param instance_id: string of instance id on AWS
        :param wait: [optional] boolean to wait for instance while initializing
        :return: True
        '''

        title = '%s.check_instance_status' % self.__class__.__name__

    # validate inputs
        input_fields = {
            'instance_id': instance_id
        }
        for key, value in input_fields.items():
            object_title = '%s(%s=%s)' % (title, key, str(value))
            self.fields.validate(value, '.%s' % key, object_title)

    # notify status check
        self.iam.printer('Querying AWS region %s for status of instance %s.' % (self.iam.region_name, instance_id))
        
    # check state of instance
        self.iam.printer_on = False
        self.check_instance_state(instance_id)
        self.iam.printer_on = True

    # check instance status
        response = self.connection.describe_instance_status(
            InstanceIds=[ instance_id ]
        )
        if not response['InstanceStatuses']:
            from time import sleep
            from timeit import timeit as timer
            sleep(1)
            response = self.connection.describe_instance_status(
                InstanceIds=[ instance_id ]
            )
            self.iam.printer(response)
        instance_status = response['InstanceStatuses'][0]['InstanceStatus']['Status']

    # wait for instance status to be ok
        if instance_status != 'ok' and wait:
            from time import sleep
            from timeit import timeit as timer
            self.iam.printer('Waiting for initialization of instance %s to stop' % instance_id, flush=True)
            delay = 3
            status_timeout = 0
            while instance_status != 'ok':
                self.iam.printer('.', flush=True)
                sleep(delay)
                t3 = timer()
                response = self.connection.describe_instance_status(
                    InstanceIds=[ instance_id ]
                )
                t4 = timer()
                response_time = t4 - t3
                if 3 - response_time > 0:
                    delay = 3 - response_time
                else:
                    delay = 0
                status_timeout += 1
                if status_timeout > 300:
                    raise Exception('\nTimeout. Failure initializing instance %s on AWS in less than 15min' % instance_id)
                instance_status = response['InstanceStatuses'][0]['InstanceStatus']['Status']
            print(' done.')
        
    # report outcome
        self.iam.printer('Instance %s is %s.' % (instance_id, instance_status))
        
        return instance_status