Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.95k stars 444 forks source link

How can I use Azure App Configuration for QueueTrigger bindings? #9598

Open gpuchtel opened 1 year ago

gpuchtel commented 1 year ago

Library name and version Microsoft.Azure.Functions.Worker 1.19.0

Query/Question I've been searching for a way to use Azure 'App Configuration' to store the connection string of an Azure Storage Queue. After hours searching on the Web I'm even more confused--there seems to be conflicting opinions on how to do this. Maybe even using Managed Identity, but that makes debugging locally a nightmare.

What I want is something like this:

[Function("foo") public void foo([QueueTrigger("bar", Connection = "FooConnection")] QueueMessage message)

Where "FooConnection" is the name of a 'key' in an Azure App Configuration from which the connection string is specified. adly, it seems that Azure only looks in the 'Configuration' of the function (or local settings if running locally).

So, is this even possible?

Note: I do 'AddAzureAppConfiguration(..) in my HostBuilder.

Thanks in advance,

I've tried to surround the name with '%' like "%FooConnection%", but I get an error saying the expression did not evaluate to a value.

I've seen posts that speak so an expression pattern, but there is not much guidance on it.

see: https://stackoverflow.com/q/77180419/8307483

bhagyshricompany commented 1 year ago

Thanks for informing . have you tried like this using Microsoft.Extensions.Configuration;

public static class ConfigurationSingleton { private static readonly IConfiguration _configuration;

static ConfigurationSingleton()
{
    _configuration = new ConfigurationBuilder()
        .AddAzureAppConfiguration()
        .Build();
}

public static string GetConnectionString()
{
    return _configuration["FooConnection"];
}

}

[Function("foo")] public void Foo([QueueTrigger("bar")] QueueMessage message) { string connectionString = ConfigurationSingleton.GetConnectionString(); // Use the connection string }

gpuchtel commented 1 year ago

No. I want the connection string to come from the attribute whereby the connection string 'should' come from my App Configuration using that name.

gvajda commented 1 year ago

This is just a lead: I remember reading that Event-based scaling doesn't allow dynamic elements in the function signature, all queues, databases, etc need to be hardcoded, constant, or app setting reference. However, it is possible when Runtime-based scaling is enabled (Configuration --> Function Runtime Settings --> Runtime Scale Monitoring). This setting has conditions and affects a wider range of behaviour so check if that matches your use case.

gpuchtel commented 1 year ago

@gvajda thanks for the replay. Not to nit-pick, but (in my view), it is an 'app setting', just that the setting is put in an Azure Configuration. I am able to resolve other configuration settings in this way, just not in an attribute definition. Sadly, this makes using 'Bicep' as a deployment tool almost useless (in my case).

gvajda commented 1 year ago

@gpuchtel 'app settings' in Azure Function App context means a very specific thing, it's not a matter of opinion. If you want to use dynamic values (including values fetched from App Config and cached in fnapp memory) then my answer applies. Unclear how this relates to bicep.