How to store function app's function keys in a key vault
Published Aug 12 2021 01:55 AM 16.7K Views
Microsoft

 

When running Azure function apps, we need function keys to access the functions. By default, the function keys of a function app are stored in a storage account, which is specified in the appsetting ‘AzureWebjobsStorage’.  You can view the keys in portal as shown below.

zhuyue_0-1628755144731.png

Fig 1. Host keys of function apps in portal

 

zhuyue_1-1628755144743.png

Fig 2. Function keys of a function in portal

 

If you go to the storage account specified in appsetting ‘AzureWebjobsStorage’, you can find the function keys in container ‘azure-webjobs-secrets’. You can see the below container where keys are stored.

 

zhuyue_2-1628755144754.png

Fig 3. The container where function keys are stored in the storage account

 

In the container, you can see the file ‘/{functionAppName}/host.json’, where function keys are stored.

zhuyue_3-1628755144768.png

Fig 4. The host.json file where function keys are written

 

Instead of a storage account, we can also choose a key vault to store function keys.  Below are the detailed steps.

 

Solution:

1. At first, add below two appsettings to the function app.

Appsetting Name

Value

AzureWebJobsSecretStorageType

keyvault

AzureWebJobsSecretStorageKeyVaultName

<key vault's name>

 

2. Enable managed identity in ‘Identity’ blade of the function app in portal.

zhuyue_4-1628755144778.png

Fig 5. Enable managed identity of a function app.

 

3. Go to ‘Access Policies’ blade of the key vault in portal, add an access policy for the function app using the app’s managed identity. You need to give the function app at least the secret management permissions.

zhuyue_5-1628755144794.png

Fig 6. The access policies blade of the key vault.

 

zhuyue_6-1628755144801.png

Fig 7. Select your function app in ‘Select principal’, and give it the secret management permissions.

 

4. You had better restart the function app to let the settings make sense immediately.  

 

5. Now when you add or delete host keys or function keys in portal, you will see the function keys in ‘secrets’ of the key vault.

zhuyue_7-1628755144809.png

Fig 8. Host/function keys stored in key vault.

 

Hope this article will be useful to you! Thanks for reading!

 

 

 

 

 

 

5 Comments
Copper Contributor

“host-functionKey-<nameOfTheKey>“ - it doesn’t include id/name of the Function App? Does it mean one Key Vault per FunctionApp?

 

Still awesome feature function app and post. I wonder… Can I rollover function key in the Key Vault and it will populate to Function App? Restart required? Have to check this soon :).

 

BTW - fix fig 7 caption: permission for “secret” not “certificate”.

Microsoft

@Tomasz Olędzki , sorry for the delay! Yes, your concerns are right. As I tested, if two function apps use the same key vault,  then in portal we can see they are using the same function keys. And also by manually modifying the secrets values or adding values in the key vault, you can also see the updates in the function app's function keys.

Copper Contributor

Note that `AzureWebJobsSecretStorageKeyVaultName` no longer works after the following change: https://github.com/Azure/Azure-Functions/issues/2048.

 

You now must use `AzureWebJobsSecretStorageKeyVaultUri`

Copper Contributor

It seems like a major oversight that some part of the function app name isn't included in the key vault secret entry names. When running multiple functions, are we expected to create a key vault for every single function?

Microsoft

Hi @KasperHoldum . To avoid conflict, we suggest you create a Key vault for a function app. The secret name contains the function name, but doesn't contain function app name. So if two function apps use the same key vault, and if they have a function with the same name, this could cause conflict.

Co-Authors
Version history
Last update:
‎Aug 17 2021 08:57 PM
Updated by: