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).
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.