You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/templates/key-vault-parameter.md
+56-25Lines changed: 56 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,21 @@
2
2
title: Key Vault secret with template
3
3
description: Shows how to pass a secret from a key vault as a parameter during deployment.
4
4
ms.topic: conceptual
5
-
ms.date: 12/17/2020
5
+
ms.date: 04/23/2021
6
6
---
7
+
7
8
# Use Azure Key Vault to pass secure parameter value during deployment
8
9
9
10
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.
10
11
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).
12
14
13
15
## Deploy key vaults and secrets
14
16
15
17
To access a key vault during template deployment, set `enabledForTemplateDeployment` on the key vault to `true`.
16
18
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.
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:
63
65
64
66
# [Azure CLI](#tab/azure-cli)
65
67
@@ -93,9 +95,9 @@ For more information about creating key vaults and adding secrets, see:
93
95
94
96
## Grant access to the secrets
95
97
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.
97
99
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.
99
101
100
102
1. Create a custom role definition JSON file:
101
103
@@ -115,6 +117,7 @@ The following procedure shows how to create a role with the minimum permission,
115
117
]
116
118
}
117
119
```
120
+
118
121
Replace "00000000-0000-0000-0000-000000000000" with the subscription ID.
119
122
120
123
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,
143
146
144
147
The samples assign the custom role to the user on the resource group level.
145
148
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).
147
150
148
151
## Reference secrets with static ID
149
152
@@ -153,7 +156,9 @@ With this approach, you reference the key vault in the parameter file, not the t
153
156
154
157
[Tutorial: Integrate Azure Key Vault in Resource Manager Template deployment](./template-tutorial-use-key-vault.md) uses this method.
155
158
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)
157
162
158
163
```json
159
164
{
@@ -189,6 +194,29 @@ The following template deploys a SQL server that includes an administrator passw
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:
193
221
194
222
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
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.
253
281
254
282
You can't dynamically generate the resource ID in the parameters file because template expressions aren't allowed in the parameters file.
255
283
@@ -367,8 +395,11 @@ The following template dynamically creates the key vault ID and passes it as a p
367
395
}
368
396
```
369
397
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
+
370
401
## Next steps
371
402
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.
374
405
- 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