3
votes

I have an Azure function that run in a docker container. This function needs to use an Azure service bus. So my function is using the attribute "ServiceBusAccount" to get the service bus connection string

    [FunctionName("MyFunc")]
    [ServiceBusAccount("ServiceBusConnectionString")]
    public async Task MyFunc(...)
    ...

In the configuration builder I run the following code to get the some values from the Azure Key Vault (including the Service bus connection string):

     var configBuilder = new ConfigurationBuilder();

     var config = configBuilder.AddAzureKeyVault($"https://{config["keyVaultName"]}.vault.azure.net", config["keyVaultClientId"], config["keyVaultSecret"])
                .Build();

The problem is that while the host is starting up I got first the warning:

"Warning: Cannot find value named 'ServiceBusConnectionString'"

and then after my function is discovered I got this error:

A host error has occurred. Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK ServiceBus connection string 'ServiceBusConnectionString' is missing or empty

My understanding is that the host environment is trying to get the service bus connection string before even starting up the function and before the configuration builder is invoked in the startup class of the project.

Is there any way to set the service bus connection string at runtime?

EDIT

If you are using Azure DevOps this should help to solve this problem https://daniel-krzyczkowski.github.io/How-to-inject-Azure-Key-Vault-secrets-in-the-Azure-DevOps-CICD-pipelines/

1
You are using servicebus trigger function or in an other kind trigger function using servicebus?George Chen
I'm using the servicebus trigger function. If I put the service bus connection string in my local.settings.json everything is fine. When I remove it from there and pull the connection string from Azure Key Vault at runtime I get the error I mentioned above.Alessandro Lallo

1 Answers

0
votes

You could set @Microsoft.KeyVault(SecretUri=Secret URI with version) in Application Setting in Function.

1.Turn on Identity of your Azure function.

2.Go to the Access Control section of your Key Vault and click on Add a role assignment blade, assign a role to your function's service principal.

3.Go to your Key Vault and click on Access Policies and then click on Add policy to your function's service principal with key or secret permission.

4.Use the Key Vault Secret in Azure Function.

For more details, you could refer to this article.