def find_orphaned_things()

in source/lambda/iot-dr-missing-device-replication/lambda_function.py [0:0]


def find_orphaned_things(c_dynamo, c_dynamo_resource, c_iot):
    table = c_dynamo_resource.Table(DYNAMODB_ERROR_TABLE)
    while True:
        if not table.global_secondary_indexes or table.global_secondary_indexes[0]['IndexStatus'] != 'ACTIVE':
            print('Waiting for index to backfill...')
            time.sleep(5)
            table.reload()
        else:
            break

    response = table.query(
        # Add the name of the index you want to use in your query.
        IndexName="action-index",
        KeyConditionExpression=Key('action').eq('create-thing'),
    )
    logger.debug('response: {}'.format(response))

    for item in response['Items']:
        item = ddb_json.loads(item)
        logger.info('item: {}'.format(item))
        if 'primary_region' in item:
            post_provision_thing(c_iot, c_dynamo, item)
        else:
            logger.warn('cannot post provision device {} - primary region unknown'.format(item['thing_name']))