Add Key Vault Secret to App Service Settings #2279
-
This is probably something simple for someone who knows what they're doing to answer 😀 I have a key vault secret (SQL connection string) that I need to add to app service settings. Does anyone have an example of how I can do this in bicep? I'm still learning this, so an example would be handy Not sure if this needs to be done in JSON still or not. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
Has the key vault and secret already been created? Or are you looking to create that as part of the bicep code? If it's the former, then you would reference the secret in a parameters file the same way you would for an ARM template and provide that parameters file to the deployment client you are using, so you would do something like:
You also need to make sure the key vault has been enabled for template deployment which you can do when you create the key vault or you can enable it at any time in the portal: If it's the latter, then you may be blocked by #1571. The only way to reference a secret dynamically is to use a nested deployment (now a module in bicep). Hope that helps, but let me know if you need more info/get stuck! |
Beta Was this translation helpful? Give feedback.
-
Here's an working example how to use App Service with Key Vault references in settings: param location string = resourceGroup().location
@secure()
param secretValue string
resource serverFarm 'Microsoft.Web/serverfarms@2020-12-01' = {
name: 'app-plan'
location: location
kind: 'linux'
sku: {
name: 'B1'
}
properties: {
reserved: true
}
}
var websiteName = 'website-${uniqueString(resourceGroup().id)}'
resource website 'Microsoft.Web/sites@2020-12-01' = {
name: websiteName
location: location
properties: {
serverFarmId: serverFarm.id
}
identity: {
type: 'SystemAssigned'
}
resource config 'config' = {
name: 'web'
properties: {
alwaysOn: true
}
}
resource settings 'config' = {
name: 'appsettings'
properties: {
SuperSecret: '@Microsoft.KeyVault(SecretUri=${keyVault::secret.properties.secretUriWithVersion})'
}
}
}
resource keyVault 'Microsoft.KeyVault/vaults@2021-04-01-preview' = {
name: 'kv-${uniqueString(resourceGroup().id)}'
location: location
properties: {
enableRbacAuthorization: true
sku: {
name: 'standard'
family: 'A'
}
tenantId: subscription().tenantId
}
resource secret 'secrets' = {
name: 'mySuperSecret'
properties: {
value: secretValue
}
}
}
var KEY_VAULT_SECRETS_USER_ROLE_GUID = subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4633458b-17de-408a-b874-0445c86b69e6')
resource keyVaultWebsiteUser 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
name: guid('SecretsUser', websiteName)
scope: keyVault
properties: {
principalId: website.identity.principalId
roleDefinitionId: KEY_VAULT_SECRETS_USER_ROLE_GUID
}
} Mind that this might not deploy successfully at first run. Problem is that enabling identity on appService can take few seconds and unfortunately the RBAC assignment might start creating before website identity is available. If it does - it will fail with message that identity was not found in your tenant. Redeployment will fix it though, however this is something worth being addressed (@alex-frankel) |
Beta Was this translation helpful? Give feedback.
-
thanks @miqm - the official doc is a pretty poor example... what the reference wants the resourceId of the secret, e.g.:
Or the simpler bicep version above... For this problem:
Add |
Beta Was this translation helpful? Give feedback.
Has the key vault and secret already been created? Or are you looking to create that as part of the bicep code?
If it's the former, then you would reference the secret in a parameters file the same way you would for an ARM template and provide that parameters file to the deployment client you are using, so you would do something like:
You also need to make sure the key vault has been enabled for template deployment which you can do when you create the key vault or you can enable it at any time in the portal:
If it's the latter, then you may be blocked by #1571. The only way to reference a secret dynamically is to use a nested deplo…