I’m trying to change an Azure function app which currently uses a System Assigned identity to connect to a KeyVault to a User Assigned Managed Identity. The UAMI has the correct permissions to the KeyVault , but I’m struggling with the sparesly documented keyVaultReferenceIdentity
property in bicep. Below is the code for my module.
param uamiId string=''
resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
name: functionAppName
location: 'uks'
kind: 'functionapp'
identity: empty(uamiId)? {
type: 'SystemAssigned'
}:{
type: 'UserAssigned'
userAssignedIdentities: {
'${uamiId}': {}
}
}
properties: {
serverFarmId: appPlansprimaryId
siteConfig: {
ftpsState: 'FtpsOnly'
minTlsVersion: '1.2'
use32BitWorkerProcess : false// 64 bit
keyVaultReferenceIdentity: uamiId
}
httpsOnly: true
keyVaultReferenceIdentity: uamiId
}
}
I don’t understand why the keyVaultReferenceIdentity is in two places. If I comment out the first reference, the app does reference it in JSON view, but only in one place, the other reference to it in the JSON config is null. If I deploy the bicep script with both references, the deployment completely screws p and it changes back to SystemAssigned in the JSON view. Are both needed? Is something else needed? There seems to be no way of doing this via the portal. The only pointer I can find is this post which suggests a powershell script https://stackoverflow.com/a/72531566/30512
2
Answers
I gt this working by using only the in the properties block:
Then specifying the correct managed identity ID in the
AZURE_CLIENT_ID
app setting and grabbing this to pass as a connection string to the keyvault. This is the Client ID, not the Principal ID:The app setting
kvUri
is the full name of the KV - eg: https://mykeyvault.vault.azure.net/Purpose of keyVaultReferenceIdentity in two places:
Firstly,
keyVaultReferenceIdentity
underproperties
block is for specifying theUser Managed Identity
that will be used by the function app during runtime for interactions with the Key Vault, like retrieving or updating secrets.The
keyVaultReferenceIdentity
undersite config
block is used to specify theUser Managed Identity
that can be used specifically for accessingKey Vault references
within the function app’s configuration settings.Note: Adding to the above, including
keyVaultReferenceIdentity
field is completely depends on your requirement. AddingkeyVaultReferenceIdentity
only in theproperties
block will also be sufficient to achieve your output.Coming to your bicep code, you need to pass identity id
keyVaultReferenceIdentity: uamiId.id
with key vault reference field but notkeyVaultReferenceIdentity: uamiId
.Modified code has given below.
Deployment succeeded:
References:
Blog | Larry Claman
Complete Bicep code for deploying an Azure Function app with Key Vault references.