algolia / algoliasearch-client-python

⚡️ A fully-featured and blazing-fast Python API client to interact with Algolia.
https://www.algolia.com/doc/api-client/getting-started/install/python/?language=python
MIT License
196 stars 67 forks source link

Getting AlgoliaUnreachableHostException("Unreachable hosts") when attempting to upload records via AWS Lambda #532

Open anu1097 opened 3 years ago

anu1097 commented 3 years ago

Description

I am facing a similar issue as reported in this Past Issue - https://github.com/algolia/algoliasearch-client-python/issues/30.

StackTrace -

raise AlgoliaUnreachableHostException("Unreachable hosts") in retryst)fNotExist": True})
END RequestId: 0d549eca-dd2e-4884-9f43-0548a6db3674
REPORT RequestId: 0d549eca-dd2e-4884-9f43-0548a6db3674  Init Duration: 0.46 ms  Duration: 4491.58 ms    Billed Duration: 4500 ms      Memory Size: 1024 MB    Max Memory Used: 1024 MB
{"errorMessage": "Unreachable hosts", "errorType": "AlgoliaUnreachableHostException", "stackTrace": ["  File \"/var/task/main.py\", line 37, in lambda_handler\n    scan_and_upload_to_algolia()\n", "  File \"/var/task/ddb_utils.py\", line 72, in scan_and_upload_to_algolia\n    update_templates_data_in_algolia(algolia_records)\n", "  File \"/var/task/algolia_utils.py\", line 21, in update_templates_data_in_algolia\n    res = templates_v4_index.save_objects(records, {\"autoGenerateObjectIDIfNotExist\": True})\n", "  File \"/var/task/algoliasearch/search_index.py\", line 69, in save_objects\n    response = self._chunk(\"addObject\", objects, request_options, False)\n", "  File \"/var/task/algoliasearch/search_index.py\", line 527, in _chunk\n    raw_responses.append(self._raw_batch(requests, request_options))\n", "  File \"/var/task/algoliasearch/search_index.py\", line 534, in _raw_batch\n    return self._transporter.write(\n", "  File \"/var/task/algoliasearch/http/transporter.py\", line 35, in write\n    return self.request(verb, hosts, path, data, request_options, timeout)\n", "  File \"/var/task/algoliasearch/http/transporter.py\", line 72, in request\n    return self.retry(hosts, request, relative_url)\n", "  File \"/var/task/algoliasearch/http/transporter.py\", line 94, in retry\n    raise AlgoliaUnreachableHostException(\"Unreachable hosts\")\n"]}% 

Code Snippet

def create_algolia_client():
    algolia_api_key = os.environ.get("INVIDEO_ALGOLIA_API_ID").strip()
    algolia_api_id = os.environ.get("INVIDEO_ALGOLIA_API_KEY").strip()
    if algolia_api_id is not "" and algolia_api_key is not "":
        algolia_client = SearchClient.create(algolia_api_id, algolia_api_key)
        logger.info("algolia search client created")
        return algolia_client
    else:
        logger.error("algolia id and key is missing, failed to create insights client")

def update_templates_data_in_algolia(records):
    algolia_client = create_algolia_client()
    templates_v4_index = algolia_client.init_index("templates_v4")
    res = templates_v4_index.save_objects(records, {"autoGenerateObjectIDIfNotExist": True})
    logger.info("Got Response from Algolia", res)

I am running this code snippet inside AWS Lambda. After reading the possible solutions in past Issue. I added urllib3, requests[security] and pyOpenSSL libraries.

Here's the pyproject.toml -

python = "3.8.5"
algoliasearch = "^2.5.0"
urllib3 = "^1.26.6"
requests[security] = 2.9.1
pyOpenSSL = 20.0.1

We have performed following checks -

  1. Api ID and Key are valid, were able to upload records from Local.
  2. Record Object is valid.
  3. Our Lambda is not in any VPC.
  4. Ensured our Lambda is able to resolve Algolia Server by performing DNS Lookup on .algolia.net.

Steps To Reproduce

  1. Run the provided snippet inside AWS Lambda with above configuration for python.
  2. Pass any valid record object and replace ID and Key with a valid one.
dylancaponi commented 1 year ago

Update: Regenerating the Write API Key solved my issue. However Algolia should still look into what causes this error state...

Same here running locally on a Mac with

    client = SearchClient.create(algolia_application_id, algolia_api_key)
    index = client.init_index(algolia_index)
    request_options = {
        "autoGenerateObjectIDIfNotExist":True
    }
fmabid commented 1 year ago

Update: Regenerating the Write API Key solved my issue. However Algolia should still look into what causes this error state...

Same here running locally on a Mac with

    client = SearchClient.create(algolia_application_id, algolia_api_key)
    index = client.init_index(algolia_index)
    request_options = {
        "autoGenerateObjectIDIfNotExist":True
    }

@dylancaponi Thanks, man! Regenerating the Write API Key solved my issue.