Below Http triggered Azure function running on consumption mode, not returning the latest secret value from Azure Keyvault even after idle time of 1 hour
Assumption as per az func scaling: Azure function app will be deleted (or) scaled down to zero when no requests received for -20mins.
If the above assumption is true, then Startup.cs will be called on the next invocation after idle time which will in turn connect to keyvault to get the latest value of secret & update appconfig.cs with latest secret value accordingly but this is not happening, even after the idle time still the old secret value is returned. This behavior implies function app may not be scaled down even after idle time.
why function app which hosts http trigger function not scaling down even after idle time?
Why this above mentioned behavior of returning stale secret value is observed? What needs to be done to overcome secret value staleness?
P.S: Currently function app restart only returning the latest secret value from key value.
Don't know here what I have misunderstood about server-less architecture of Azure function?
On Startup.cs
[assembly: FunctionsStartup(typeof(Startup))]
namespace SampleAzureFunction
{
public class Startup : FunctionsStartup
{
private AppConfig _appConfig;
public IConfigurationRoot _configuration;
public override void Configure(IFunctionsHostBuilder builder)
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(currentDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
_appConfig = new AppConfig();
_configuration = configurationBuilder.Build();
ConfigurationBinder.Bind(_configuration, _appConfig);
//here c# code to Update appConfig=>DBConfig=>PrimaryKey with secret value mentioned in keyvault using "KeyVaultClient" class in Microsoft.Azure.KeyVault namespace
.....
.....
builder.Services.AddSingleton<IAppConfig>(_appConfig);
}
}
}
On MyClass.cs
namespace SampleAzureFunction
{
public class MyClass
{
private readonly IAppConfig _appConfig;
public MyClass(IAppConfig appConfig)
{
_appConfig = appConfig;
}
[FunctionName("myfunc")]
public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
{
var secret= _appConfig.PrimaryKey;
return secret; //always return old secret not the new secret update in keyvault even after fun app idletime of >1hour
}
}
}
appsettings.cs
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": ""
},
"AzureKeyVaultConfig: {
"VaultUrl": ""
},
"DBConfig": {
"DBUri": "",
"PrimaryKey": "", //not configured in appsettings but in keyvault
"DBName": ""
}
}