Search code examples
python-3.xazureazure-functionsazure-cosmosdb

Azure Functions v2 Python - CosmosDB input decorator


I am trying to write a very basic function in an azure function app using the python v2 programming model. All I want it to do is connect to a CosmosDB account and return all the documents listed in a given container called 'systems'. It'll return a filtered set of them in the future but for now there's only one document in it so no filtering required.

This is the function at present based on adapting these examples from azure:

import azure.functions as func
import logging
import json

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.function_name(name="system")
@app.route(route="system")
@app.cosmos_db_input(arg_name="documents", 
                     database_name="database",
                     container_name="systems",
                     connection_string_setting="DATABASE_CONNECTION_STRING")
def system(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    return func.HttpResponse(
        json.dumps(documents),
        status_code=200
    )

When i try and run the function using the Azure Function Core Tools (Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit) Function Runtime Version: 4.21.1.20667

I get the below error, I cannot for the life of me figure out what information i need to give it for the 'connection' argument. Based on all the azure demos I should only need to provide the connection string which is available in the local.settings.json file with the name DATABASE_CONNECTION_STRING.

[2023-10-02T10:11:56.430Z] Worker failed to index functions
[2023-10-02T10:11:56.432Z] Result: Failure
Exception: TypeError: BindingApi.cosmos_db_input() missing 1 required positional argument: 'connection'        
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 338, in _handle__functions_metadata_request
    fx_metadata_results = self.index_functions(function_path)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 607, in index_functions
    indexed_functions = loader.index_function_app(function_path)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call
    return func(*args, **kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\loader.py", line 151, in index_function_app
    imported_module = importlib.import_module(module_name)
  File "C:\Users\gcookman\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\gcookman\Code\KPMG-DAI-API\function_app.py", line 9, in <module>
    @app.cosmos_db_input(arg_name="documents",

Any help or guidance appreciated :)


Solution

  • You have a mix of configs. connection_string_setting is for the old extension while container_name is for the new/current one.

    Based on the error, you seem to tbe using the new one.

    Change connection_string_setting to connection. The Function docs seem a bit off (https://learn.microsoft.com/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv4&pivots=programming-language-python#configuration) because they don't have _ but the names are the right ones.