1

I am trying to create an azure function cosmosdbtrigger .My cosmosdb is in a different resource id as compared to my azure function. However My function is not getting triggered.

Is there any restriction that the azure function and cosmosdb should be in the same resource id. If not is there any additional setting to be done for a different resource id.

My azure function is on python running on a linux app service. From the azure documentation i came to know, i cannot mix app services from windows and linux as the current limitation.

Azure Documentation on Current Limitation

I need to use an azure function Python to check azure cosomos db change feed.

Here is my function.json used for connecting to a cosmosdb collection trigger..

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases1",
      "connectionStringSetting": "devcosmosdb_DOCUMENTDB",
      "databaseName": "devcosmosdb",
      "collectionName": "testCollection",
      "createLeaseCollectionIfNotExists": "true"
    }
  ]
}
slaveCoder
  • 519
  • 2
  • 17
  • 46
  • >Is there any restriction that the azure function and cosmosdb should be in the same >resource id. If not is there any additional setting to be done for a different resource id. No – Νικόλαος Μανωλακος Jun 16 '20 at 17:35
  • >My azure function is on python running on a linux app service. From the azure >documentation i came to know, i cannot mix app services from windows and linux as the current limitation. Is you Azure Function or Did you deploy your own serverless framwork on a Azure App Service? – Νικόλαος Μανωλακος Jun 16 '20 at 17:37
  • There is no limitation, you should have an Azure Function Configuration called `devcosmosdb_DOCUMENTDB` pointing to the connection string of the cosmos db account, did you do that? – Matias Quaranta Jun 16 '20 at 19:12

3 Answers3

2

There is no such limitation.

Please check the databaseName, collectionName and connectionStringSetting again.

If you have already deployed the function to Azure portal. You need to add the connectionStringSetting to Application Settings. In your scenario, you should add the connectionString like this

enter image description here

You can find the connectionString under Keys part of your cosmosdb account.

enter image description here

Also, please check the FireWall settings.

enter image description here

Tony Ju
  • 14,891
  • 3
  • 17
  • 31
1

I would like to add on some more useful information related to this topic. You might be the old me looking at the Monitor logs waiting for something to appear when you use the default template of the __init__.py to get update on the inserted document to your CosmosDB.

May I refer you to this link - https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-configure-cosmos-db-trigger#enabling-logging

You need to edit the host.json file to enable logging for CosmosDB before anything useful will appear in your logs.

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "Host.Triggers.CosmosDB": "Trace"
    }
  }
}
Seng Wee
  • 534
  • 9
  • 20
0

I had for some reason changed the name in the bindings section of the function.json. If this doesn't match with the parameter name in your function definition, it will not fire.

For example, name in here:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents", <<<<<<<<<<<<<
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "CosmosDBConnectionString",
      "databaseName": "dbname",
      "collectionName": "collname",
      "createLeaseCollectionIfNotExists": true
    }
  ]
}

Must match the variable name in the main method of __init__.py when using Python as runtime (this should be similar for other languages):

import azure.functions as func
"""
          Must match with this 
              |
              |
              V
"""

def main(documents: func.DocumentList):
     # do something smart with the list



Gerard
  • 2,832
  • 3
  • 27
  • 39