azure-functions-host icon indicating copy to clipboard operation
azure-functions-host copied to clipboard

Azure Cosmos DB Input bindings come with incorrect fields on Azure Portal

Open mburakeker opened this issue 2 years ago • 4 comments

When you create an Azure Cosmos DB Input in your function on Azure Portal, the template generates a binding with incorrect fields like connectionStringSetting and collectionName which should be connection and containerName.

Investigative information

  • Timestamp: 2023-09-24T13:38:46.408
  • Function App version: 4.26.1.21286
  • Function App name: week3-coursera
  • Function name(s) (as appropriate): HttpTrigger1
  • Invocation ID: e64eb6c0-1575-41a6-a145-5042ba4db216
  • Region: Germany West Central

Repro steps

Provide the steps required to reproduce the problem:

  1. Sign in to Azure Portal
  2. Create an Azure Cosmos DB Account
  3. Create a new Azure Cosmos DB Container
  4. Create a Function App
  5. Create an HTTP Trigger Function
  6. Navigate to Integrations tab in your function
  7. Add a CosmosDB Input
  8. Open file logs
  9. Try to get data from CosmosDB using the function
  10. Observe the error log that says: Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

Or you can simply follow this guide on Microsoft Learn as newly created CosmosDB bindings are faulty right now: https://learn.microsoft.com/en-us/training/modules/chain-azure-functions-data-using-bindings/5-read-data-with-input-bindings-portal-lab?pivots=javascript

Expected behavior

The function should be able to connect to Azure Cosmos DB.

Actual behavior

The function does not connect to Azure Cosmos DB and throws the following error: Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

Known workarounds

You can manually change the field names in bindings as below:

  • Change connectionStringSetting to connection
  • Change collectionName to containerName

Related information

Provide any related information

  • Programming language used Javascript
  • Links to source https://learn.microsoft.com/en-us/training/modules/chain-azure-functions-data-using-bindings/5-read-data-with-input-bindings-portal-lab?pivots=javascript
  • Bindings used Azure CosmosDB Input
Source

Bindings

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "documentUrls",
      "direction": "in",
      "type": "cosmosDB",
      "connectionStringSetting": "####_DOCUMENTDB",
      "databaseName": "new-database",
      "collectionName": "new-container",
      "id": "{id}",
      "partitionKey": "/id"
    }
  ]
}

Code

module.exports = function (context, req) {

    var bookmark = context.bindings.bookmark

    if(bookmark){
        context.res = {
        body: { "url": bookmark.url },
        headers: {
            'Content-Type': 'application/json'
        }
        };
    }
    else {
        context.res = {
            status: 404,
            body : "No bookmarks found",
            headers: {
            'Content-Type': 'application/json'
            }
        };
    }

    context.done();
};

mburakeker avatar Sep 24 '23 14:09 mburakeker

Thanks for update.Can you try this and check its working.{ "bindings": [ { "authLevel": "function", "name": "req", "type": "httpTrigger", "direction": "in", "methods": [ "get", "post" ] }, { "name": "$return", "type": "http", "direction": "out" }, { "name": "outputDocument", "direction": "out", "type": "cosmosDB", "connection": "afreen-cosmosdb_DOCUMENTDB", "databaseName": "outDatabase", "containerName": "MyCollection", "createIfNotExists": true, "partitionKey": "/id" } ] }

bhagyshricompany avatar Sep 25 '23 05:09 bhagyshricompany

Thanks @bhagyshricompany for your response. I am aware of the workaround, I already mentioned it twice in the issue. I opened this issue to report a bug.

mburakeker avatar Sep 25 '23 06:09 mburakeker

I am experiencing the same issue on Azure Cosmos DB Output bindings resulting in "Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting."

The mentioned workaround of renaming the field names does not work since they are reverted automatically.

SimonSkotheimsvik avatar Oct 12 '23 08:10 SimonSkotheimsvik

Thank you so much @mburakeker The microsoft documentation is super careless. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv4&pivots=programming-language-javascript#http-trigger-look-up-id-from-route-data-javascript

databaseName: 'ToDoItems',
collectionName: 'Items',
id: '{id}',
partitionKey: '{partitionKeyValue}',
connectionStringSetting: 'CosmosDBConnection',

instead of

databaseName: "ToDoItems", containerName: "Items", id: "{id}", partitionKey: "{partitionKeyValue}", connection: "CosmosDBConnection",

kswat avatar Apr 22 '24 08:04 kswat