user33276346
user33276346

Reputation: 1739

Azure Function to use connection from Azure Key Vault local development

We are developing the function locally and although we can use the ConfigurationBuilder().AddAzureKeyVault() in the startup to retrieve the Key Vault secrets, we are struggling to make the Queue trigger to get the connection string from the Key Vault. As can be seen below, the connection is currently retrieved from a AzureWebJobsStorage key present in the local.settings.json:

    [FunctionName("myFunction")]
    public async Task MyFunction([QueueTrigger("aQueue", Connection = "AzureWebJobsStorage")] Car myCar)
    {
        await getCarAsync(myCar.Name);
    }

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=....",

Upvotes: 2

Views: 2014

Answers (2)

Tracy
Tracy

Reputation: 670

There appears to be a documented issue with triggers, in some cases. (Taken from this article.)

enter image description here

Upvotes: 1

Tom Biddulph
Tom Biddulph

Reputation: 524

If you're using dependency injection with either FunctionsStartup or IWebJobsStartup you can set the connection string from keyvault like this.

  1. Set the Connection property on the QueueTriggerAttribute to an empty string eg

     [FunctionName("myFunction")]
     public async Task MyFunction([QueueTrigger("aQueue", Connection = "")] Car myCar)
     {
         await getCarAsync(myCar.Name);
     }
    
  1. In your startup class use PostConfigure<QueueAttribute>() to set the connection string value, eg

    public class Startup : FunctionsStartup
    {
      public override void Configure(IFunctionsHostBuilder builder)
      {
         builder.Services.PostConfigure<QueueTriggerAttribute>(x =>
         {
             x.Connection = ReadQueueConnectionStringFromKeyvault();
         });
      }
    }
    

Another alternative to consider is KeyVault references

Upvotes: 3

Related Questions