Set Cosmos DB Binding Connection String At Runtime

siddharth bansal 326 Reputation points
2023-06-08T07:44:17.9533333+00:00

I am using Cosmos Db trigger azure function ,i want to get the ConnectionStringSetting value from key vault ,so i added below code to my startup.cs file

configBuilder.AddAzureKeyVault("https://myvault.vault.net");
var config = configBuilder.Build();
builder.Services.PostConfigure<CosmosDBOptions>(options =>
{
    options.ConnectionString = config["CosmosDbUrl"]; // CosmosDbUrl is a secret in the Key Vault
});

i have one output binding

Code with Cosmos Db Trigger -

public async Task Run([CosmosDBTrigger(
            databaseName: "%databaseName%",
            collectionName: "%ContainerName%",
            ConnectionStringSetting = "",
            LeaseCollectionName = "%LeaseContainerName%",
            LeaseCollectionPrefix ="%LeaseCollectionPrefix%")]IReadOnlyList<Document> input, ILogger log)

the above code work fine(keeping the ConnectionStringSetting field as empty) , but when i add an output binding , it throws error -"Unable to resolve the value for property 'CosmosDBAttribute.ConnectionStringSetting'. Make sure the setting exists and has a valid value"
Code with cosmos db trigger & output binding -

public async Task Run([CosmosDBTrigger(
            databaseName: "%databaseName%",
            collectionName: "%ContainerName%",
            ConnectionStringSetting = "",
            LeaseCollectionName = "%LeaseContainerName%",
            LeaseCollectionPrefix ="%LeaseCollectionPrefix%")]IReadOnlyList<Document> input, ILogger log, [CosmosDB(
            databaseName: "%databaseName%",
            collectionName: "%ContainerName2%",
            ConnectionStringSetting = ""

        )] IAsyncCollector<object> output)

kindly suggest how to resolve the issue.

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,293 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,029 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,643 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,336 Reputation points
    2023-06-08T15:05:41.6433333+00:00

    siddharth bansal Thanks for posting your question in Microsoft Q&A. Based on your description, you are looking to get Cosmos DB connecting string from Azure Key Vault in your Azure Function and faced an error with output binding. As per doc Attributes, ConnectionStringSetting is the name of the app setting that bindings use to connect to Cosmos DB and empty string is not valid.

    Here are the steps you can follow to achieve this:

    1. Enable Managed Identity for your Function app (reference: https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Chttp)
    2. Create an access policy in Key Vault for the managed identity (also necessary network access per doc)
    3. Add a new app setting (example CosmosDBConnName) with Reference syntax pointing to secret name/Uri (CosmosDbUrl in your example) and set ConnectionStringSetting as CosmosDBConnName. (if you are testing it in local, you would need to create this setting in local.setting.json (sample project by one of our MVP).

    More detailed step by step instructions can be found here and doc Use Key Vault references for App Service and Azure Functions. I hope this helps and let me 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.