title | description | services | documentationcenter | author | manager | editor | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Key Vault secret with Azure Resource Manager template | Microsoft Docs |
Shows how to pass a secret from a key vault as a parameter during deployment. |
azure-resource-manager,key-vault |
na |
tfitzmac |
timlt |
tysonn |
azure-resource-manager |
na |
article |
na |
na |
11/09/2017 |
tomfitz |
When you need to pass a secure value (like a password) as a parameter during deployment, you can retrieve the value from an Azure Key Vault. You retrieve the value by referencing the key vault and secret in your parameter file. The value is never exposed because you only reference its key vault ID. You do not need to manually enter the value for the secret each time you deploy the resources. The key vault can exist in a different subscription than the resource group you are deploying to. When referencing the key vault, you include the subscription ID.
When creating the key vault, set the enabledForTemplateDeployment property to true. By setting this value to true, you permit access from Resource Manager templates during deployment.
To create a key vault and secret, use either Azure CLI or PowerShell. Notice that the key vault is enabled for template deployment.
For Azure CLI, use:
vaultname={your-unique-vault-name}
password={password-value}
az group create --name examplegroup --location 'South Central US'
az keyvault create \
--name $vaultname \
--resource-group examplegroup \
--location 'South Central US' \
--enabled-for-template-deployment true
az keyvault secret set --vault-name $vaultname --name examplesecret --value $password
For PowerShell, use:
$vaultname = "{your-unique-vault-name}"
$password = "{password-value}"
New-AzureRmResourceGroup -Name examplegroup -Location "South Central US"
New-AzureRmKeyVault `
-VaultName $vaultname `
-ResourceGroupName examplegroup `
-Location "South Central US" `
-EnabledForTemplateDeployment
$secretvalue = ConvertTo-SecureString $password -AsPlainText -Force
Set-AzureKeyVaultSecret -VaultName $vaultname -Name "examplesecret" -SecretValue $secretvalue
Whether you are using a new key vault or an existing one, ensure that the user deploying the template can access the secret. The user deploying a template that references a secret must have the Microsoft.KeyVault/vaults/deploy/action
permission for the key vault. The Owner and Contributor roles both grant this access.
The template that receives a key vault secret is like any other template. That's because you reference the key vault in the parameter file, not the template. For example, the following template deploys a SQL database that includes an administrator password. The password parameter is set to a secure string. But, the template does not specify where that value comes from.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminLogin": {
"type": "string"
},
"adminPassword": {
"type": "securestring"
},
"sqlServerName": {
"type": "string"
}
},
"resources": [
{
"name": "[parameters('sqlServerName')]",
"type": "Microsoft.Sql/servers",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"tags": {},
"properties": {
"administratorLogin": "[parameters('adminLogin')]",
"administratorLoginPassword": "[parameters('adminPassword')]",
"version": "12.0"
}
}
],
"outputs": {
}
}
Now, create a parameter file for the preceding template. In the parameter file, specify a parameter that matches the name of the parameter in the template. For the parameter value, reference the secret from the key vault. You reference the secret by passing the resource identifier of the key vault and the name of the secret. In the following example, the key vault secret must already exist, and you provide a static value for its resource ID.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminLogin": {
"value": "exampleadmin"
},
"adminPassword": {
"reference": {
"keyVault": {
"id": "/subscriptions/<subscription-id>/resourceGroups/examplegroup/providers/Microsoft.KeyVault/vaults/<vault-name>"
},
"secretName": "examplesecret"
}
},
"sqlServerName": {
"value": "<your-server-name>"
}
}
}
Now, deploy the template and pass in the parameter file. For Azure CLI, use:
az group create --name datagroup --location "Central US"
az group deployment create \
--name exampledeployment \
--resource-group datagroup \
--template-file sqlserver.json \
--parameters @sqlserver.parameters.json
For PowerShell, use:
New-AzureRmResourceGroup -Name datagroup -Location "Central US"
New-AzureRmResourceGroupDeployment `
-Name exampledeployment `
-ResourceGroupName datagroup `
-TemplateFile sqlserver.json `
-TemplateParameterFile sqlserver.parameters.json
The previous section showed how to pass a static resource ID for the key vault secret. However, in some scenarios, you need to reference a key vault secret that varies based on the current deployment. In that case, you cannot hard-code the resource ID in the parameters file. Unfortunately, you cannot dynamically generate the resource ID in the parameters file because template expressions are not permitted in the parameters file.
To dynamically generate the resource ID for a key vault secret, you must move the resource that needs the secret into a nested template. In your master template, you add the nested template and pass in a parameter that contains the dynamically generated resource ID. Your nested template must be available through an external URI. The rest of this article assumes you have added the preceding template to a storage account, and it is available through the URI - https://<storage-name>.blob.core.windows.net/templatecontainer/sqlserver.json
.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vaultName": {
"type": "string"
},
"vaultResourceGroup": {
"type": "string"
},
"secretName": {
"type": "string"
},
"adminLogin": {
"type": "string"
},
"sqlServerName": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2015-01-01",
"name": "nestedTemplate",
"type": "Microsoft.Resources/deployments",
"properties": {
"mode": "incremental",
"templateLink": {
"uri": "https://<storage-name>.blob.core.windows.net/templatecontainer/sqlserver.json",
"contentVersion": "1.0.0.0"
},
"parameters": {
"adminPassword": {
"reference": {
"keyVault": {
"id": "[resourceId(subscription().subscriptionId, parameters('vaultResourceGroup'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"
},
"secretName": "[parameters('secretName')]"
}
},
"adminLogin": { "value": "[parameters('adminLogin')]" },
"sqlServerName": {"value": "[parameters('sqlServerName')]"}
}
}
}],
"outputs": {}
}
Deploy the preceding template, and provide values for the parameters.
- For general information about key vaults, see Get started with Azure Key Vault.
- For complete examples of referencing key secrets, see Key Vault examples.