2

I have a MongoDB running in a AKS and a load balancer created for exposing it outside AKS cluster. I also have set the bindIP in mongod.conf using below command

sed "s,\\(^[[:blank:]]*bindIp:\\) .*,\\1 0.0.0.0," /etc/mongod.conf
mongod --config C:\mongodb\conf\mongodb.conf

I have an Azure function written in python to connect to the db.

I am new to python and just trying out the basic code to connect to the mongodb.

import logging

import azure.functions as func
import pymongo


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    #if name:
    elif name:
        uri = "mongodb://User:User001@20.81.34.105:27017/testdb"
        client = pymongo.MongoClient(uri)

        db = client.test
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

When I am trying to run the code I am getting the below error, any help is appreciated.

Result: Failure Exception: ModuleNotFoundError: No module named 'pymongo'. 
Troubleshooting Guide: https://aka.ms/functions-modulenotfound 
Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", 
line 315, in _handle__function_load_request func = loader.load_function( 
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", 
line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", 
line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", 
line 85, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py", 
line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/HttpTrigger1/__init__.py", 
line 4, in <module> import pymongo
0sVoid
  • 2,547
  • 1
  • 10
  • 25
Sutapa
  • 21
  • 1
  • do you have `pymongo` in your `requirements.txt`? – 3nws Aug 08 '22 at 09:15
  • Yes, my requiremnt.txt is . # Function dependencies, for example: # package>=version pymongo >=3.12.0 – Sutapa Aug 08 '22 at 09:19
  • Does this answer your question? [Azure Function ModuleNotFoundError in Python script](https://stackoverflow.com/questions/61707234/azure-function-modulenotfounderror-in-python-script) – Ecstasy Aug 08 '22 at 09:37
  • [Troubleshoot ModuleNotFoundError](https://learn.microsoft.com/en-us/azure/azure-functions/recover-python-functions?tabs=vscode#troubleshoot-modulenotfounderror) – Ecstasy Aug 08 '22 at 09:38

1 Answers1

1

There are several reasons the error might be related to as described here:

  1. How do you deploy (VSCode, Azure Devops) is the requirements.txt part of the code deployed?
  2. is the pymongo compatible with python version of your azure function? it is compatible according to this https://pypi.org/project/pymongo/3.9.0/.
  3. the function has WEBSITE_RUN_FROM_PACKAGE set to 1?

you need to troubleshoot and check the guide in here: https://aka.ms/functions-modulenotfound

Anass Kartit
  • 2,017
  • 15
  • 22
  • I figured out the problem. The MongoDB running in a AKS which I was accessing was installed using helm BitNami charts and by default it is accessible inside the AKS cluster. It didn't expose any external IP for service on installation. Also I was not able to expose it outside cluster and while accessing it through various ways like python azure functions or any other way, it was giving misleading errors. I did the installation of MongoDB again using StatefulSet and Yamls. This time on installation the service created an external IP and with that the errors were gone. – Sutapa Aug 24 '22 at 06:12
  • The link is broken – Anass Kartit Aug 24 '22 at 07:53
  • Link -https://saibharath005.medium.com/mongodb-in-azure-kubernetes-4b06c2bc152b – Sutapa Aug 24 '22 at 09:47