What should I do about the Azure Function Http trigger not connecting to Cosmos DB?

Wang Kangli 75 Reputation points
2023-08-23T07:28:10.7166667+00:00

I am learning the module called "Chain Azure Functions together using input and output bindings".

The previous modules are learning without any problem and can successfully access the http trigger via url.

But in this module, I have successfully created cosmosDB and can successfully execute query: "select * from c".

After adding cosmosDB to the Input of the http trigger, then I get an error with status code 500.

The log shows ERROR:

Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

Here is 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();
};

Here is function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "bookmark",
      "direction": "in",
      "type": "cosmosDB",
      "connectionStringSetting": "my-database_DOCUMENTDB",
      "databaseName": "func-io-learn-db",
      "collectionName": "Bookmarks",
      "id": "id",
      "partitionKey": "/id"
    }
  ]
}

I have tried many times (including deleting and recreating the resource) and still have this problem!

What should I do?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,293 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,447 questions
{count} votes

4 answers

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,146 Reputation points
    2023-08-23T14:07:23.3933333+00:00

    Wang Kangli Thanks for posting your question in Microsoft Q&A. Based on the description, you follow the training module Chain Azure Functions together using input and output bindings and faced error: Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting. when trying Cosmos Input binding.

    If you use 4.2.0 (or later) of Microsoft.Azure.WebJobs.Extensions.CosmosDB in your project file (.csproj), then attributes collectionName, ConnectionStringSetting are not available (only available in older version (v3)) and hence you faced the error.

    Check out Attributes doc for the properties of both Extension 4.x+ and Functions 2.x+ versions.

    User's image

    Modify your function.json as below and validate.

    {
          "name": "bookmark",
          "direction": "in",
          "type": "cosmosDB",
          "connection": "my-database_DOCUMENTDB",
          "databaseName": "func-io-learn-db",
          "containerName": "Bookmarks",
          "id": "{id}",
          "partitionKey": "{id}"
        }
    

    If you still face any issues, share your project file reference info, function runtime version, Core Tools version (if local), and how request was made (URL, request body etc.) that would help in understanding it better. I hope this helps with your question and let us know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    20 people found this answer helpful.

  2. Thomas Pilt 60 Reputation points
    2024-01-22T16:46:14.1866667+00:00

    Nathan named it, that it's not so obvious in Muthu's answer, though kudos go to him, since he answered it correctly. I did not see the comment to Raheels response at first. To make it more explicit (since I didn't spot it on first sight either) and to help anyone coming across this issue too, here's what is different:
    good: connection, bad: connectionStringSetting

    good: containerName , bad: collectionName

    11 people found this answer helpful.

  3. Raheel Siddiqui 0 Reputation points
    2023-09-21T05:05:49.9566667+00:00

    I am new to Azure dev and facing same issue. I am not VS or VS Code just trying to run the cod fro Test/Run. not sure how to check the version of Microsoft.Azure.Webjobs.Extions.CosmosDB in Azure. Can you help me out resolving this issue?


  4. rdp 0 Reputation points
    2024-05-02T17:42:32.95+00:00

    When I was working on the

    Module name: Chain Azure Functions together using input and output bindings

    I got the same error only after adding the output bindings.

    Error message:

    2024-05-02T16:44:00.267 [Error] Executed 'Functions.HttpTrigger3' (Failed, Id=3adfe2ef-2c11-411c-ab66-46371138cc42, Duration=10ms)Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

    To resolve it, In Function's environment variable, had only two values namely AccountEndpoint and AccountKey. But later I added 2 more values as well namely, Database value and containerId.

    AccountEndpoint=https://azcosmosdbaccount1.documents.azure.com:443/;AccountKey=ajcAIrkTSDDBqicy8NcWkpUaFjGtfa4az4c7Vq1fX3EyHKKeRzyAp4nholIbbzPLZA7NzW8bFJ9wACDbtpmdXg==;

    Database=func-io-learn-db;containerId=Bookmarks;

    After restarting the function, following was the error message,

    New Error Message: 2024-05-02T17:37:01.582 [Error] Executed 'Functions.HttpTrigger3' (Failed, Id=6241cece-4917-4180-aa78-257c4427133b, Duration=10ms)Value cannot be null. (Parameter 'containerId')

    Furthermore, following was function.json file:

    {

      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },
        {
          "name": "bookmark",
          "direction": "in",
          "type": "cosmosDB",
          "partitionKey": "{id}",
          "methods": [],
          "databaseName": "func-io-learn-db",
          "collectionName": "Bookmarks",
          "connectionStringSetting": "azcosmosdbaccount1_DOCUMENTDB",
          "id": "{id}"
        },
        {
          "name": "newbookmark",
          "direction": "out",
          "type": "cosmosDB",
          "partitionKey": "/id",
          "methods": [],
          "databaseName": "func-io-learn-db",
          "collectionName": "Bookmarks",
          "connectionStringSetting": "azcosmosdbaccount1_DOCUMENTDB"
        },
        {
          "name": "newmessage",
          "direction": "out",
          "type": "queue",
          "queueName": "bookmarks-post-process",
          "methods": [],
          "connection": "learn72620e65a50a4980b5_STORAGE"
        }
      ]
    }