Per environment configurations for Azure Functions?

Viewed 582

For local development I can get away doing Environment.GetEnvironmentVariable("ApiKey") and in my local.settings.json file I would have a JSON property ApiKey: "abc123".

But when I publish my function from Visual Studio or from Azure Pipeline into Staging or Production. They should be reading config values as per environment and some from KeyVault as well.

I have worked with Consul on-prem installation and it was a breeze. I am not sure what should be the right approach in case of Azure. Use Azure App Configuration service with labels for environments or go into each function instance and manually add configurations and map some with KeyVault?

1 Answers

host.json

You don't need to use a KeyVault to store these secrets. Open your Function App in the Azure portal, then

Settings -> Application Settings -> New Application Setting

where you fill in

Name: ApiKey
Value: thankscasper

and you can access them like you did locally.

KeyVault

If you do want to use a KeyVault, then set up a system-assigned managed identity for the Function App enter image description here

Give the managed identity get rights for keys under the KeyVault's access policies

If your stored secret is called X, you can access it from the keyvault with

Environment.GetEnvironmentVariable("X", EnvironmentVariableTarget.Process);
Related