1

I'm trying to understand the integration between Azure Functions and Key Vault. In the application settings file on the portal, if I reference a KV endpoint, does the Function runtime retrieve the result once and cache it locally or does it hit the endpoint every time the config key is referenced? In other words, I'm trying to understand if any changes in KV will require the Function app to be restarted or not.

user246392
  • 2,661
  • 11
  • 54
  • 96

4 Answers4

2

Update: Versions no longer required for Key Vault references in App Service and Azure Functions

If a version is not specified in the reference, then the app will use the latest version that exists in Key Vault. When newer versions become available, such as with a rotation event, the app will automatically update and begin using the latest version within one day. Any configuration changes made to the app will cause an immediate update to the latest versions of all referenced secrets.


If you use App Service Key Vault References, you will need to update the configuration value in the Azure Portal. Restarting will not change anything. This is because you are referencing an actual secret version. If you update the secret, you will get a new version.

Versions are currently required. When rotating secrets, you will need to update the version in your application configuration

See also Azure Function App use latest version of Key Vault Secret via Application Settings

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
2

For now, you don't need to restart your Azure function manually if you update a key value in Key Vault. The azure function will restart automatically for you to load all new values. Azure functions load values defined in application settings at the start stage, if you use App Service Key Vault References in your Azure function, the key value will also be loaded from Key Vault at the start stage. If you modify application settings on Azure Portal, your Azure function will get restarted to reload all settings: enter image description here

These are my test steps:

My code is simple, just get the key value from application settings:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string key = Environment.GetEnvironmentVariable("key");


    return new OkObjectResult(key);
}

My key config in application settings: enter image description here

As you can see I did not involve SecretVersion, so that I can get the latest value in KeyVault. If you involve SecretVersion in application settings, as @Alex AIT said, you should modify application settings with the latest SecretVersion as well.

Before I modify the key value: enter image description here

Then I update the key4demo value in Key vault as: 78910 enter image description here The Azure function reply 503 which means it is restarting : enter image description here

After few seconds, my function replys me with the latest value in KV: enter image description here

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • Interesting that this works in spite of the official docs. I only found one GitHub Issue which calls it "not supported" / "not defined". Do you have further sources for this behavior? Even though I don't like the fact that it restarts my app (downtime), it is still interesting to see. https://github.com/MicrosoftDocs/azure-docs/issues/41917 – Alex AIT Jan 12 '21 at 06:37
  • @AlexAIT, LOL , actually I have been using this way for a long time, seems there is no offcial doc mentioned but it does work. – Stanley Gong Jan 12 '21 at 07:08
  • Seems like it is now officially supported ;-) https://azure.microsoft.com/en-us/updates/versions-no-longer-required-for-key-vault-references-in-app-service-and-azure-functions/ – Alex AIT Feb 14 '21 at 15:23
-1

no, no need to restart function or anything. Key Vault lives a separate life.

nobodyfromhell
  • 321
  • 1
  • 10
-1

Microsoft Docs

If a version is not specified in the reference, then the app will use the latest version that exists in the key vault. When newer versions become available, such as with a rotation event, the app will automatically update and begin using the latest version within 24 hours. The delay is because App Service caches the values of the key vault references and refetches it every 24 hours. Any configuration changes to the app that results in a site restart causes an immediate refetch of all referenced secrets.

Zidan
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '22 at 16:19