Skip to content

Commit 8ec8160

Browse files
authored
Merge pull request #251124 from polatengin/enpolat/adding-bicepparam-getsecret-function
adding `az.getSecret` function documentation for bicepparam files
2 parents 1b1bf75 + 379da74 commit 8ec8160

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

articles/azure-resource-manager/bicep/bicep-functions-parameters-file.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Bicep functions - parameters file
3-
description: Describes the functions used in the Bicep parameters files.
3+
description: This article describes the Bicep functions to be used in Bicep parameter files.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
66
ms.date: 06/05/2023
@@ -10,6 +10,58 @@ ms.date: 06/05/2023
1010

1111
Bicep provides a function called `readEnvironmentVariable()` that allows you to retrieve values from environment variables. It also offers the flexibility to set a default value if the environment variable does not exist. This function can only be used in the `.bicepparam` files. For more information, see [Bicep parameters file](./parameter-files.md).
1212

13+
## getSecret
14+
15+
`getSecret(subscriptionId, resourceGroupName, keyVaultName, secretName, secretVersion)`
16+
17+
Returns a secret from an [Azure Key Vault](../../key-vault/secrets/about-secrets.md). Use this function to pass a secret to a secure string parameter of a Bicep file.
18+
19+
> [!NOTE]
20+
> You can also use the [keyVaultName.getSecret(secretName)](./bicep-functions-resource.md#getsecret) function from within a `.bicep` file.
21+
22+
```bicep
23+
using './main.bicep'
24+
25+
param secureUserName = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretUserName')
26+
param securePassword = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretPassword')
27+
```
28+
29+
You'll get an error if you use this function with string interpolation.
30+
31+
A [namespace qualifier](bicep-functions.md#namespaces-for-functions) (`az`) can be used, but it's optional, because the function is available from the _default_ Azure Namespace.
32+
33+
### Parameters
34+
35+
| Parameter | Required | Type | Description |
36+
|:--- |:--- |:--- |:--- |
37+
| subscriptionId | Yes | string | The ID of the subscription that has the key vault resource. |
38+
| resourceGroupName | Yes | string | The name of the resource group that has the key vault resource. |
39+
| keyVaultName | Yes | string | The name of the key vault. |
40+
| secretName | Yes | string | The name of the secret stored in the key vault. |
41+
| secretVersion | No | string | The version of the secret stored in the key vault. |
42+
43+
### Return value
44+
45+
The value for the secret.
46+
47+
### Example
48+
49+
The following `.bicepparam` file has a `securePassword` parameter that will have the latest value of the _\<secretName\>_ secret.
50+
51+
```bicep
52+
using './main.bicep'
53+
54+
param securePassword = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretPassword')
55+
```
56+
57+
The following `.bicepparam` file has a `securePassword` parameter that will have the value of the _\<secretName\>_ secret, but it's pinned to a specific _\<secretValue\>_.
58+
59+
```bicep
60+
using './main.bicep'
61+
62+
param securePassword = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretPassword', 'exampleSecretVersion')
63+
```
64+
1365
## readEnvironmentVariable()
1466

1567
`readEnvironmentVariable(variableName, [defaultValue])`

articles/azure-resource-manager/bicep/bicep-functions-resource.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ Built-in policy definitions are tenant level resources. For an example of deploy
106106

107107
Returns a secret from an Azure Key Vault. Use this function to pass a secret to a secure string parameter of a Bicep module.
108108

109+
> [!NOTE]
110+
> `az.getSecret(subscriptionId, resourceGroupName, keyVaultName, secretName, secretVersion)` function can be used in `.bicepparam` files to retrieve key vault secrets. For more information, see [getSecret](./bicep-functions-parameters-file.md#getsecret).
111+
109112
You can only use the `getSecret` function from within the `params` section of a module. You can only use it with a `Microsoft.KeyVault/vaults` resource.
110113

111114
```bicep
@@ -117,7 +120,7 @@ module sql './sql.bicep' = {
117120
}
118121
```
119122

120-
You'll get an error if you attempt to use this function in any other part of the Bicep file. You'll also get an error if you use this function with string interpolation, even when used in the params section.
123+
You get an error if you attempt to use this function in any other part of the Bicep file. You also get an error if you use this function with string interpolation, even when used in the params section.
121124

122125
The function can be used only with a module parameter that has the `@secure()` decorator.
123126

@@ -137,7 +140,7 @@ The secret value for the secret name.
137140

138141
### Example
139142

140-
The following Bicep file is used as a module. It has an `adminPassword` parameter defined with the `@secure()` decorator.
143+
The following Bicep file is used as a module. It has an `adminPassword` parameter defined with the `@secure()` decorator.
141144

142145
```bicep
143146
param sqlServerName string

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ The following functions are available for working with objects. All of these fun
123123

124124
## Parameters file functions
125125

126+
The [getSecret function](./bicep-functions-parameters-file.md) is available in Bicep to get secure value from a KeyVault. This function is in the `az` namespace.
127+
126128
The [readEnvironmentVariable function](./bicep-functions-parameters-file.md) is available in Bicep to read environment variable values. This function is in the `sys` namespace.
127129

128130
## Resource functions

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ module sql './sql.bicep' = {
202202
}
203203
```
204204

205+
Also, `getSecret` function (or with the namespace qualifier `az.getSecret`) can be used in a `.bicepparam` file to retrieve the value of a secret from a key vault.
206+
207+
```bicep
208+
using './main.bicep'
209+
210+
param secureUserName = getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretUserName', 'exampleSecretVersion')
211+
param securePassword = az.getSecret('exampleSubscription', 'exampleResourceGroup', 'exampleKeyVault', 'exampleSecretPassword')
212+
```
213+
205214
## Reference secrets in parameters file
206215

207216
If you don't want to use a module, you can reference the key vault directly in the parameters file. The following image shows how the parameters file references the secret and passes that value to the Bicep file.

articles/azure-resource-manager/bicep/scenarios-secrets.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ module exampleModule 'module.bicep' = {
8080
}
8181
```
8282

83+
### Use a key vault in a .bicepparam file
84+
85+
When you use `.bicepparam` file format, you can provide secure values to parameters by using [the `getSecret` function](bicep-functions-parameters-file.md#getsecret).
86+
87+
Reference the KeyVault by providing the subscription ID, resource group name, and key vault name. You can get the value of the secret by providing the secret name. You can optionally provide the secret version. If you don't provide the secret version, the latest version is used.
88+
89+
```bicep
90+
using './main.bicep'
91+
92+
param secureUserName = az.getSecret('<subscriptionId>', '<resourceGroupName>', '<keyVaultName>', '<secretName>', '<secretVersion>')
93+
param securePassword = az.getSecret('<subscriptionId>', '<resourceGroupName>', '<keyVaultName>', '<secretName>')
94+
```
95+
8396
## Work with secrets in pipelines
8497

8598
When you deploy your Azure resources by using a pipeline, you need to take care to handle your secrets appropriately.

0 commit comments

Comments
 (0)