Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

The 'CosmosDBTrigger' function is in error: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

Open yalcinsabancelebi opened this issue 11 months ago • 2 comments

I'm trying to run my CosmosDB trigger locally, but when trying to run with func host start, I get this error: The 'CosmosDBTrigger' function is in error: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

I found a similar issue here: #2447, but even after trying the suggested solution and using the sample code from the Azure CosmosDB trigger documentation, I couldn't resolve it.

Here are my files:

function_app.py

import logging
import os
import azure.functions as func
from main import app as main
import logging
import azure.functions as func

app = func.AsgiFunctionApp(app=main, http_auth_level=func.AuthLevel.ANONYMOUS)

@app.function_name(name="CosmosDBTrigger")
@app.cosmos_db_trigger(
                    arg_name="documents", 
                    database_name="<db_name_here>",
                    collection_name="<container_name_here>",
                    create_if_not_exists=True,
                    lease_container_name="leases",
                    create_lease_container_if_not_exists="true",
                    connection_string_setting=os.getenv("COSMOS_DB_CONNECTION_STRING"))
def test_function(documents: func.DocumentList) -> str:
    if documents:
        logging.info('Document id: %s', documents[0]['id'])

host.json

{
  "version": "2.0",
  "extensions": {
    "http": {
        "routePrefix": ""
    },
    "cosmosDB": {
      "connectionMode": "Gateway",
      "userAgentSuffix": "cosmostest"
  }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

local.setting.json

  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<myaccountnamehere>;AccountKey=<myaccountkeyhere>EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "<mydbname>_DOCUMENTDB": "AccountEndpoint=https://<myfuncname>.documents.azure.com:443/;AccountKey=<myaccountkey>"
  }
}

function.json

    "scriptFile": "function_app.py",
    "bindings": [
      {
        "type": "cosmosDBTrigger",
        "name": "azcosmosdb",
        "direction": "in",
        "collectionName": "<my_container_name_here>",
        "databaseName": "<my_db_name_here>",
        "connectionStringSetting": "<my_db_name_here>_DOCUMENTDB",
        "leaseCollectionName": "leasesDemo",
        "createLeaseCollectionIfNotExists": true
      },
      {
        "type": "httpTrigger",
        "name": "req",
        "direction": "in",
        "authLevel": "anonymous"
      },
      {
        "type": "http",
        "name": "res",
        "direction": "out"
      }
    ]
  }

Just want the trigger to run whenever there is any CRUD operation in Cosmos DB, that's all.

Thanks!

yalcinsabancelebi avatar Dec 12 '24 16:12 yalcinsabancelebi

@paulbatum @mgravell @daxian-dbw @kshyju

yalcinsabancelebi avatar Jan 01 '25 18:01 yalcinsabancelebi

Here from C# land, but if this is a 'new' functions app, which version / model are you using? I would assume you are using the latest.

The first thing that sticks out to me is collection_name has changed to container_name in Functions 4.X models.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-trigger?tabs=python-v1%2Cisolated-process%2Cextensionv4%2Cnodejs-v4&pivots=programming-language-python#example

Also, the docs site seems a bit broken at the moment, you'll need to select that you're using V1 programming model to find the Functions v4.X+ programming model.

Screenshot: Image

EntityAdam avatar Jan 05 '25 04:01 EntityAdam