Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #2569

Open
yalcinsabancelebi opened this issue Dec 12, 2024 · 2 comments

Comments

@yalcinsabancelebi
Copy link

yalcinsabancelebi commented Dec 12, 2024

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
Copy link
Author

yalcinsabancelebi commented Jan 1, 2025

@EntityAdam
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants