azureazure-functionsazure-cosmosdbcosmosdbtrigger

Azure Function cosmosDB trigger managed identity not working


I created an isolated Azure functions for cosmosDB trigger. I'm using Microsoft.Azure.Functions.Worker.Extensions.CosmosDB --version 4.0.0-preview2 to use managed identity. Below is my function.

[Function("CosmosDBTrigger")]
    public void Run([CosmosDBTrigger(
        databaseName: "testdata",
        containerName: "test",
        Connection = "connect",
        LeaseContainerName = "leases")] IReadOnlyList<MyDocument> input)
    {}

local.settings.json

{
  "Values": {
   connect__accountEndpoint": "https://testdatacosmosdb.documents.azure.com:443/"
}}

I get the error "Cosmos DB connection configuration 'connect' does not exist. Make sure that it is a defined App Setting. I'm not sure if it happening because of isolated azure functions


Solution

  • You are missing the credentials. See:

    All Azure Functions bindings that are going to use MSI need the <your attribute value>__credential in the configuration besides the Service Specific properties.

    For Cosmos DB, the Service Specific property is accountEndpoint.

    To summarize, you need to have both.

    In your local.settings.json file:

    {
       "connect" : {
          "accountEndpoint": "https://testdatacosmosdb.documents.azure.com:443/",
          "credential" : "managedidentity"
    
        }
    }
    
    

    Once deployed in Azure, you need to add them to your Functions App Configuration, for that you can use the underscore notation:

    The version you are using of the package is old and only supports adding these on the "Connection Strings" section of the Function App Configuration.

    "connect__accountEndpoint": "https://testdatacosmosdb.documents.azure.com:443/"
    
    "connect__credential": "managedidentity"
    

    Here is also an Azure Friday episode of the whole scenario end to end: https://www.youtube.com/watch?v=w002dYaP9mw