Azure / Azure-Functions

1.1k stars 189 forks source link

The 'COSMOSDBTrigger' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.COSMOSDBTrigger'. #2454

Closed meer-khan closed 3 months ago

meer-khan commented 4 months ago

Hello Azure community,

I've spent an entire day grappling with the configuration of Azure Functions triggered by Cosmos DB for MongoDB. While attempting to set it up locally using both the CLI and VSCode, I encountered various challenges. I want to highlight that the Azure documentation lacks specificity, particularly when it comes to configuring functions for Cosmos DB triggers (specifically for changes in Cosmos DB). The majority of the official documentation primarily focuses on HTTPTriggers.

I would greatly appreciate it if someone could provide a comprehensive, step-by-step guide—from creating an Azure Function app to triggering it through Cosmos DB. I am specifically looking for detailed instructions using VSCode or the CLI, covering both local testing and the deployment of the function for PYTHON v2 only.

NOTE: I am using free tier account of Azure I have a storage service on it I have Azure function app I have cosmosDB for Mongodb

Even on youtube tutorials, people only discussed HTTPTrigger for Python v2, and very few people discussed cosmosdb trigger for v2

Currently, I am facing the following issue while running the local function through func start, and I would be grateful for any assistance in resolving it and providing me a concrete step-by-step guide to creating an azure function triggered through cosmos DB.

 Error indexing method 'Functions.COSMOSDBTrigger'
[2024-03-05T08:34:11.937Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.COSMOSDBTrigger'. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cannot create container information for users in database TestDB with lease leases in database TestDB : Cosmos DB connection configuration  "<CONNECTION STRING>".  does not exist. Make sure that it is a defined App Setting. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cosmos DB connection configuration 

function_app.py

import datetime
import json
import logging

app = func.FunctionApp()

@app.cosmos_db_trigger(arg_name="azcosmosdb", container_name="users",
                        database_name="TestDB", connection="<CONNECTION STRING>",
                        create_lease_container_if_not_exists=True, lease_container_name="leases") 
def COSMOSDBTrigger(azcosmosdb: func.DocumentList):
    logging.info('Python CosmosDB triggered.')
    print("HELLOOO MY FIREND")

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "DatabaseName": "TestDB",    
  }
}

.vscode/extensions.json

{
    "recommendations": [
        "ms-azuretools.vscode-azurefunctions"
    ]
}
owboateng commented 3 months ago

I have been able to reproduce your use case and the reason the trigger is not working is because Azure CosmosDB triggers do not yet support the MongoDB API. Refer to issue: https://github.com/Azure/azure-webjobs-sdk-extensions/issues/816

meer-khan commented 3 months ago

Hi @owboateng, Thankyou for your response, yes you are right, I investigated this problem for long time and your answer cleared all my concerns and confusions. I believe azure should not name the trigger as CosmosDB Trigger, it should be Azure CosmosDB for NoSQL trigger, because cosmosDB has alot of databases and triggers does not work with all of them. Now I am closing this issue on your accurate response. Thanks again.