Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cosmos DB connection configuration 'CosmosDB' does not exist

193 Views Asked by At

I have a blob trigger function. this code snippet from init.py:

credential = DefaultAzureCredential()
endpoint = os.environ["connect__accountEndpoint"]
client = CosmosClient(
    url = endpoint,
    credential = credential
)
database_name = os.environ["cosmosDatabase"]
container_name = os.environ["cosmosColl"]
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)

This is my local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "",
    "AzureWebJobsSecretStorageType": "files",
    "cosmosDatabase": "",
    "cosmosColl": "",
    "connect__accountEndpoint": "",
    "connect__clientId": "",
    "BlobConnStr": ""
  }
}

This is my function.json :

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "contracts/{name}",
      "connection": "BlobConnStr"
    },
    {
      "direction": "out",
      "type": "cosmosDB",
      "name": "doc",
      "databaseName": "%cosmosDatabase%",
      "collectionName": "%cosmosColl%",
      "connect__accountEndpoint": "connect__accountEndpoint",
      "createIfNotExists": true
    }
  ]
}

I am getting this error:

System.Private.CoreLib: Exception while executing function: Functions.contracts_blob_trigger. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

I have a problem in Configuration part between json files. How can I handle this error?

1

There are 1 best solutions below

6
Matias Quaranta On

First issue is connect__accountEndpoint. You are using a special notation in Azure Functions that normally means that there is nested JSON:

{
  "connect": 
  {
     "accountEndpoint":"...."
  }
}

In the Values section. But your Values section does not have a connection node with the accountEndpoint and clientId attributes.

But looking at what you are using, you seem to be attempting to use MSI Authentication (accountEndpoint + clientId) which is incompatible with the version of the Extension you seem to be using.

MSI Auth is supported in Extension and Bundles 4.x, for which the names of the attributes and configurations are different (collectionName no longer exists, its containerName).

Please make sure you are using the latest bundle (https://learn.microsoft.com/azure/azure-functions/migrate-cosmos-db-version-3-version-4?tabs=isolated-process&pivots=programming-language-python), and then your function.json would look like:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "contracts/{name}",
      "connection": "BlobConnStr"
    },
    {
      "direction": "out",
      "type": "cosmosDB",
      "name": "doc",
      "databaseName": "%cosmosDatabase%",
      "containerName": "%cosmosColl%",
      "connection": "connect",
      "createIfNotExists": true
    }
  ]
}

And your host.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "",
    "AzureWebJobsSecretStorageType": "files",
    "cosmosDatabase": "",
    "cosmosColl": "",
    "connect": {
         "accountEndpoint":"",
         "clientId":""
    },
    "BlobConnStr": ""
  }
}