0

I am unable to trigger a response when I add documents in Azure Cosmos DB. I have applied solutions from the following links Unable to run cosmosDB trigger locally with Azure Function Core Tools Azure Functions: CosmosDBTrigger not triggering in Visual Studio

I am trying to create a cosmosDB trigger that gets triggered when a new document is added to the collection. I created a resourcegroup appRG and then added an Azure function App and a Azure cosmos DB. I used python as my programming language not python V2 and created the function. My __init__.py as follows:

import logging
import azure.functions as func
def main(documents: func.DocumentList) -> str:
    if documents:
         logging.info('Document id: %s', documents[0]['id'])

Basic code that gets created when you create an azure function trigger. I followed this link: Azure CosmosDB Python Trigger . I am using Function 2.x, as such the following function.json gets created

{  
   "scriptFile": "__init__.py",
   "bindings": [
   {
  "type": "cosmosDBTrigger",
  "name": "documents",
  "direction": "in",
  "leaseCollectionName": "leases",
  "connectionStringSetting": "app_DOCUMENTDB",
  "databaseName": "test",
  "collectionName": "story",
  "createLeaseCollectionIfNotExists": true
  }
 ]
 }

The connectionStringSetting "app_DocumentDB" starts with AccountEndpoint as explained in connection string. Before deployment, I debugged and executed the function, and it was triggered . I got the errors during debugging as explained in this link but when I executed the function it displayed the following result

Document id: sample
 [2023-08-18T08:01:16.039Z] Executed 'Functions.CosmosTrigger1' (Succeeded, Id=8144c581-a6df- 
 4f93-a6bb-e5aeccd3f45b, Duration=94ms)

When I deploy the function to the function app, I also upload the settings, so in the function APP configuration--->Application settings, app_DocumentDB is stored with the same value as shown in the local.settings.json file. github issue

But when I insert document into the story collection nothing gets triggered, as nothing is shown under the monitor Invocation (I have waited for 5 minutes). Monitor Invocation

I also tried adding the cosmosDB primary connection string into the app configuration and saving it in the variable app_conn

app_conn = mongodb://d-app:......................

and using that variable in "connectionStringSetting":"app_conn" in the local.settings.json file before deployment.

One thing that I would like to mention is that as per Azure Function Trigger not working for a different resource id of cosmosdb, my primary connection string in azure cosmosDB however does not start with AccountEndpoint

However, still the function is not getting triggered.

Please help, I have read the github issues and the azure documentation and followed most of the strategies mentioned in the stackoverflow. But since I am new to this I may have missed some key configurations.

Any help will be appreciated. Thanks in advance.

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6
Abhinav Choudhury
  • 319
  • 2
  • 3
  • 15
  • fyi [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) Also you included what appears to be nearly all of your database key in that image (I would change your key asap). – David Makogon Aug 18 '23 at 12:55
  • 1
    Thank you @DavidMakogon. It was the key for an old cosmosDB and app, that I had already deleted. However I have removed the keys from the question – Abhinav Choudhury Aug 18 '23 at 13:14
  • are you adding the correct connection string(should start with AccountEndpoint)[image](https://i.imgur.com/uFTIL00.png) after deployment in functionApp's application setting? I have tried the same configuration and it is getting triggered for me after deployment – Ikhtesam Afrin Aug 24 '23 at 07:14

1 Answers1

0

I have reproduced the issue at my environment and got the expected result

Primarily I created a default python Cosmos DB triggered function in Visual studio code like you did.

__init__.py

import  logging
import  azure.functions  as  func

def  main(documents: func.DocumentList) -> str:
if  documents:
logging.info('Document id: %s', documents[0]['id'])

local.settings

{
"IsEncrypted": false,

"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python",
"afreencosmosdb_DOCUMENTDB": "Your connection string"
}
}

This function is working as expected locally, then I published it to Function App.

Added connection string as this

Even I was unable to see the logs in monitor section, then I checked the logs in the associated application insight. My function was throwing some connectivity error because I had added the connection string in encrypted format(*****) in Configuration . After resolving the error, I am able to see the successful requests both in Application Insight and in Monitor.

Please Note: only successful or failed requests will be visible in function's Monitor. If your function is throwing any exception, it will only visible in Application Insight.

Please use application insight to get more exposer on all kind (Traces, Exception, Request etc.) of logs.

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6
  • Thank you for the answer. So I looked at the Application Insight. And yes it was giving exceptions. "The listener for function 'Functions.CosmosTrigger1' was unable to start. Microsoft.Azure.DocumentDB.Core: Sql api is not supported for this database account". So is your database azure cosmosDB for mongoDB or azure cosmosDB for Nosql? Because I found this answer that says "Azure Cosmos DB trigger bindings are only supported for Azure Cosmos DB for NoSQL, and they do not work with Azure Cosmos DB for MongoDB" – Abhinav Choudhury Sep 02 '23 at 14:46
  • My database is cosmosDB NoSql – Ikhtesam Afrin Sep 02 '23 at 18:15
  • 1
    Refer https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function, it says `Azure Cosmos DB bindings are only supported to use with cosmosDB for NoSql` – Ikhtesam Afrin Sep 02 '23 at 18:25