Azure Function - CosmosDbTrigger - Bind variables from Keyvault and App configuration instead of appsettings

Mecomber, Michael 1 Reputation point
2021-11-11T00:44:41.01+00:00

I'm using .NET 6 and Azure functions v4. I would like to use Keyvault and App configuration (for non secure configuration) for bind variables in CosmosDbTrigger but have been unsuccessful.

For example:

        [Function("Function1")]
        public async Task Run([CosmosDBTrigger(
            databaseName: "%COSMOS_RAW_DATABASE%",
            containerName: "%COSMOS_RAW_CONTAINER_NAME%",
            Connection = "CONNECTION-COSMOS",
            LeaseContainerName = "leases",
            CreateLeaseContainerIfNotExists = true)] IReadOnlyList<object> input)
        {

databaseName, containerName, and Connection all should be driven from configuration. Ideally, databaseName and containerName will come from App Configuration and CONNECTION-COSMOS will come from Keyvault.

This function definition works if I have everything in local.settings.json (run locally) and in app settings of the function resource when deployed. However, I want to avoid storing in the settings in the resources appsettings.

When I try to set up Keyvault and App configuration as configuration providers, it works just fine but the functions seem to launch before I get in to Program.cs / Main so the configuration is not available and I get messages like this (below).

Does anyone have any guidance on how to set this up using configuration from Keyvault and App Configuration?

NOTE: I'm observing the same behavior both locally and deployed

Azure Functions Core Tools
Core Tools Version: 4.0.3971 Commit hash: d0775d487c93ebd49e9c1166d5c3c01f3c76eaaf (64-bit)
Function Runtime Version: 4.0.1.16815

[2021-11-11T00:37:51.846Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Function1'. Microsoft.Azure.WebJobs.Host: '%COSMOS_RAW_DATABASE%' does not resolve to a value.
[2021-11-11T00:37:51.862Z] Error indexing method 'Functions.Function1'
[2021-11-11T00:37:51.865Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Function1'. Microsoft.Azure.WebJobs.Host: '%COSMOS_RAW_DATABASE%' does not resolve to a value.
[2021-11-11T00:37:51.866Z] Function 'Functions.Function1' failed indexing and will be disabled.

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,131 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,313 questions
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
209 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2021-11-11T12:39:13.53+00:00

    @Mecomber, Michael This can be achieved by customizing configuration sources but this affects scaling in the consumption and premium tiers. The other option is to use KeyVault References instead.


  2. Jaliya Udagedara 2,731 Reputation points MVP
    2021-11-12T02:52:19.347+00:00

    For Azure Functions Isolated Model (Out-of-process), you can just whatever ASP.NET Core does.

    Install Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet package

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    
    namespace FunctionApp1;
    
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureAppConfiguration(config =>
                {
                    IConfigurationRoot settings = config.Build();
                    var connection = settings.GetConnectionString("ConnectionString");
                    config.AddAzureAppConfiguration(connection);
                })
                .Build();
    
            host.Run();
        }
    }