Skip to content

Commit a82086f

Browse files
authored
Merge pull request #155723 from davidsmatlak/ds-key-vault-parms
Updates key vault parameter file doc
2 parents c3aa7fc + adeb04e commit a82086f

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

articles/azure-resource-manager/templates/key-vault-parameter.md

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
title: Key Vault secret with template
33
description: Shows how to pass a secret from a key vault as a parameter during deployment.
44
ms.topic: conceptual
5-
ms.date: 12/17/2020
5+
ms.date: 04/23/2021
66
---
7+
78
# Use Azure Key Vault to pass secure parameter value during deployment
89

910
Instead of putting a secure value (like a password) directly in your template or parameter file, you can retrieve the value from an [Azure Key Vault](../../key-vault/general/overview.md) during a deployment. 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. The key vault can exist in a different subscription than the resource group you're deploying to.
1011

11-
This article focuses on the scenario of passing a sensitive value in as a template parameter. It doesn't cover the scenario of setting a virtual machine property to the URL of a certificate in a Key Vault. For a quickstart template of that scenario, see [Install a certificate from Azure Key Vault on a Virtual Machine](https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-winrm-keyvault-windows).
12+
This article's focus is how to pass a sensitive value as a template parameter. The article doesn't cover how to set a virtual machine property to a certificate's URL in a key vault.
13+
For a quickstart template of that scenario, see [Install a certificate from Azure Key Vault on a Virtual Machine](https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-winrm-keyvault-windows).
1214

1315
## Deploy key vaults and secrets
1416

1517
To access a key vault during template deployment, set `enabledForTemplateDeployment` on the key vault to `true`.
1618

17-
If you already have a Key Vault, make sure it allows template deployments.
19+
If you already have a key vault, make sure it allows template deployments.
1820

1921
# [Azure CLI](#tab/azure-cli)
2022

@@ -30,7 +32,7 @@ Set-AzKeyVaultAccessPolicy -VaultName ExampleVault -EnabledForTemplateDeployment
3032

3133
---
3234

33-
To create a new Key Vault and add a secret, use:
35+
To create a new key vault and add a secret, use:
3436

3537
# [Azure CLI](#tab/azure-cli)
3638

@@ -59,7 +61,7 @@ $secret = Set-AzKeyVaultSecret -VaultName ExampleVault -Name 'ExamplePassword' -
5961

6062
---
6163

62-
As the owner of the key vault, you automatically have access to creating secrets. If the user working with secrets isn't the owner of the key vault, grant access with:
64+
As the owner of the key vault, you automatically have access to create secrets. If the user working with secrets isn't the owner of the key vault, grant access with:
6365

6466
# [Azure CLI](#tab/azure-cli)
6567

@@ -93,9 +95,9 @@ For more information about creating key vaults and adding secrets, see:
9395

9496
## Grant access to the secrets
9597

96-
The user who deploys the template must have the `Microsoft.KeyVault/vaults/deploy/action` permission for the scope of the resource group and key vault. The [Owner](../../role-based-access-control/built-in-roles.md#owner) and [Contributor](../../role-based-access-control/built-in-roles.md#contributor) roles both grant this access. If you created the key vault, you're the owner so you have the permission.
98+
The user who deploys the template must have the `Microsoft.KeyVault/vaults/deploy/action` permission for the scope of the resource group and key vault. The [Owner](../../role-based-access-control/built-in-roles.md#owner) and [Contributor](../../role-based-access-control/built-in-roles.md#contributor) roles both grant this access. If you created the key vault, you're the owner and have the permission.
9799

98-
The following procedure shows how to create a role with the minimum permission, and how to assign the user
100+
The following procedure shows how to create a role with the minimum permission, and how to assign the user.
99101

100102
1. Create a custom role definition JSON file:
101103

@@ -115,6 +117,7 @@ The following procedure shows how to create a role with the minimum permission,
115117
]
116118
}
117119
```
120+
118121
Replace "00000000-0000-0000-0000-000000000000" with the subscription ID.
119122

120123
2. Create the new role using the JSON file:
@@ -143,7 +146,7 @@ The following procedure shows how to create a role with the minimum permission,
143146

144147
The samples assign the custom role to the user on the resource group level.
145148

146-
When using a Key Vault with the template for a [Managed Application](../managed-applications/overview.md), you must grant access to the **Appliance Resource Provider** service principal. For more information, see [Access Key Vault secret when deploying Azure Managed Applications](../managed-applications/key-vault-access.md).
149+
When using a key vault with the template for a [Managed Application](../managed-applications/overview.md), you must grant access to the **Appliance Resource Provider** service principal. For more information, see [Access Key Vault secret when deploying Azure Managed Applications](../managed-applications/key-vault-access.md).
147150

148151
## Reference secrets with static ID
149152

@@ -153,7 +156,9 @@ With this approach, you reference the key vault in the parameter file, not the t
153156

154157
[Tutorial: Integrate Azure Key Vault in Resource Manager Template deployment](./template-tutorial-use-key-vault.md) uses this method.
155158

156-
The following template deploys a SQL server that includes an administrator password. The password parameter is set to a secure string. But, the template doesn't specify where that value comes from.
159+
The following template deploys a SQL server that includes an administrator password. The password parameter is set to a secure string. But the template doesn't specify where that value comes from.
160+
161+
# [JSON](#tab/json)
157162

158163
```json
159164
{
@@ -189,6 +194,29 @@ The following template deploys a SQL server that includes an administrator passw
189194
}
190195
```
191196

197+
# [Bicep](#tab/bicep)
198+
199+
```bicep
200+
param adminLogin string
201+
202+
@secure()
203+
param adminPassword string
204+
205+
param sqlServerName string
206+
207+
resource sqlServer 'Microsoft.Sql/servers@2020-11-01-preview' = {
208+
name: sqlServerName
209+
location: resourceGroup().location
210+
properties: {
211+
administratorLogin: adminLogin
212+
administratorLoginPassword: adminPassword
213+
version: '12.0'
214+
}
215+
}
216+
```
217+
218+
---
219+
192220
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:
193221

194222
In the following parameter file, the key vault secret must already exist, and you provide a static value for its resource ID.
@@ -198,25 +226,25 @@ In the following parameter file, the key vault secret must already exist, and yo
198226
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
199227
"contentVersion": "1.0.0.0",
200228
"parameters": {
201-
"adminLogin": {
202-
"value": "exampleadmin"
203-
},
204-
"adminPassword": {
205-
"reference": {
206-
"keyVault": {
229+
"adminLogin": {
230+
"value": "exampleadmin"
231+
},
232+
"adminPassword": {
233+
"reference": {
234+
"keyVault": {
207235
"id": "/subscriptions/<subscription-id>/resourceGroups/<rg-name>/providers/Microsoft.KeyVault/vaults/<vault-name>"
208-
},
209-
"secretName": "ExamplePassword"
210-
}
211-
},
212-
"sqlServerName": {
213-
"value": "<your-server-name>"
236+
},
237+
"secretName": "ExamplePassword"
214238
}
239+
},
240+
"sqlServerName": {
241+
"value": "<your-server-name>"
242+
}
215243
}
216244
}
217245
```
218246

219-
If you need to use a version of the secret other than the current version, use the `secretVersion` property.
247+
If you need to use a version of the secret other than the current version, include the `secretVersion` property.
220248

221249
```json
222250
"secretName": "ExamplePassword",
@@ -249,7 +277,7 @@ New-AzResourceGroupDeployment `
249277

250278
## Reference secrets with dynamic ID
251279

252-
The previous section showed how to pass a static resource ID for the key vault secret from the parameter. However, in some scenarios, you need to reference a key vault secret that varies based on the current deployment. Or, you may want to pass parameter values to the template rather than create a reference parameter in the parameter file. In either case, you can dynamically generate the resource ID for a key vault secret by using a linked template.
280+
The previous section showed how to pass a static resource ID for the key vault secret from the parameter. In some scenarios, you need to reference a key vault secret that varies based on the current deployment. Or you may want to pass parameter values to the template rather than create a reference parameter in the parameter file. The solution is to dynamically generate the resource ID for a key vault secret by using a linked template.
253281

254282
You can't dynamically generate the resource ID in the parameters file because template expressions aren't allowed in the parameters file.
255283

@@ -367,8 +395,11 @@ The following template dynamically creates the key vault ID and passes it as a p
367395
}
368396
```
369397

398+
> [!NOTE]
399+
> As of Bicep version 0.3.255, a parameter file is needed to retrieve a key vault secret because the `reference` keyword isn't supported. There's work in progress to add support and for more information, see [GitHub issue 1028](https://github.com/Azure/bicep/issues/1028).
400+
370401
## Next steps
371402

372-
- For general information about key vaults, see [What is Azure Key Vault?](../../key-vault/general/overview.md).
373-
- For complete examples of referencing key secrets, see [Key Vault examples](https://github.com/rjmax/ArmExamples/tree/master/keyvaultexamples).
403+
- For general information about key vaults, see [What is Azure Key Vault?](../../key-vault/general/overview.md)
404+
- For complete examples of referencing key secrets, see [key vault examples](https://github.com/rjmax/ArmExamples/tree/master/keyvaultexamples) on GitHub.
374405
- For a Microsoft Learn module that covers passing a secure value from a key vault, see [Manage complex cloud deployments by using advanced ARM template features](/learn/modules/manage-deployments-advanced-arm-template-features/).

0 commit comments

Comments
 (0)