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/bicep/variables.md
+86-35Lines changed: 86 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,32 +3,32 @@ title: Variables in Bicep
3
3
description: Describes how to define variables in Bicep
4
4
ms.topic: conceptual
5
5
ms.custom: devx-track-bicep
6
-
ms.date: 04/28/2025
6
+
ms.date: 05/30/2025
7
7
---
8
8
9
9
# Variables in Bicep
10
10
11
11
This article describes how to define and use variables in your Bicep file. You use variables to simplify your Bicep file development. Rather than repeating complicated expressions throughout your Bicep file, you define a variable that contains the complicated expression. Then, you use that variable as needed throughout your Bicep file.
12
12
13
-
Resource Manager resolves variables before starting the deployment operations. Wherever the variable is used in the Bicep file, Resource Manager replaces it with the resolved value.
14
-
15
-
You're limited to 512 variables in a Bicep file. For more information, see [Template limits](../templates/best-practices.md#template-limits).
13
+
Resource Manager resolves variables before starting the deployment operations. Wherever the variable is used in the Bicep file, Resource Manager replaces it with the resolved value. You're limited to 512 variables in a Bicep file. For more information, see [Template limits](../templates/best-practices.md#template-limits).
16
14
17
15
## Define variables
18
16
19
-
The syntax for defining a variable is:
17
+
A variable can't have the same name as a parameter, module, or resource. You can add one or more decorators for each variable. For more information, see Use [decorators](#use-decorators).
18
+
19
+
### Untyped variables
20
+
21
+
When you define a variable without specifying a data type, the type is inferred from the value. The syntax for defining an untyped variable is:
20
22
21
23
```bicep
22
24
@<decorator>(<argument>)
23
25
var <variable-name> = <variable-value>
24
26
```
25
27
26
-
A variable can't have the same name as a parameter, module, or resource.
27
-
28
-
Notice that you don't specify a [data type](data-types.md) for the variable. The type is inferred from the value. The following example sets a variable to a string.
28
+
The following example sets a variable to a string.
29
29
30
30
```bicep
31
-
var stringVar = 'example value'
31
+
var stringVar = 'preset variable'
32
32
```
33
33
34
34
You can use the value from a parameter or another variable when constructing the variable.
@@ -68,7 +68,7 @@ var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().i
68
68
output uniqueStorageName string = storageName
69
69
```
70
70
71
-
The preceding example returns a value like the following:
71
+
The preceding example returns a value like the following output:
72
72
73
73
```json
74
74
"uniqueStorageName": {
@@ -77,6 +77,81 @@ The preceding example returns a value like the following:
77
77
}
78
78
```
79
79
80
+
### Typed variables
81
+
82
+
Starting with [Bicep CLI version 0.36.X](https://github.com/Azure/bicep/releases/tag/v0.36.1), Bicep supports **typed variables**, where you explicitly declare the data type of a variable to ensure type safety and improve code clarity. The benefits of typed variables:
83
+
84
+
-**Error detection**: The Bicep compiler validates that assigned values match the declared type, catching errors early.
85
+
-**Code clarity**: Explicit types make it clear what kind of data a variable holds.
86
+
-**Intellisense support**: Tools like Visual Studio Code provide better autocompletion and validation for typed variables.
87
+
-**Refactoring safety**: Ensures that changes to variable assignments don’t inadvertently break type expectations.
88
+
89
+
To define a typed variable, use the `var` keyword followed by the variable name, the type, and the assigned value:
90
+
91
+
```bicep
92
+
var <variable-name> <data-type> = <variable-value>
93
+
```
94
+
95
+
The following examples show how to define typed variables:
96
+
97
+
```bicep
98
+
var resourceName string = 'myResource'
99
+
var instanceCount int = 3
100
+
var isProduction bool = true
101
+
var tags object = { environment: 'dev' }
102
+
var subnets array = ['subnet1', 'subnet2']
103
+
```
104
+
105
+
For `object` types, you can define a schema to enforce a specific structure. The compiler ensures the object adheres to the defined schema.
106
+
107
+
```bicep
108
+
var config {
109
+
name: string
110
+
count: int
111
+
enabled: bool
112
+
} = {
113
+
name: 'myApp'
114
+
count: 5
115
+
enabled: true
116
+
}
117
+
```
118
+
119
+
The following example uses typed variables with decorators to enforce constraints:
120
+
121
+
```bicep
122
+
@description('The environment to deploy to')
123
+
@allowed(['dev', 'test', 'prod'])
124
+
param environment string = 'dev'
125
+
126
+
var instanceCount int = environment == 'prod' ? 5 : 2
-`instanceCount` is typed as `int` and uses a conditional expression.
150
+
-`resourcePrefix` is typed as `string`.
151
+
-`tags` is typed as `object` with a specific structure.
152
+
153
+
## Use iterative loops
154
+
80
155
You can use iterative loops when defining a variable. The following example creates an array of objects with three properties.
81
156
82
157
```bicep
@@ -141,30 +216,6 @@ Markdown-formatted text can be used for the description text.
141
216
142
217
Use `@export()` to share the variable with other Bicep files. For more information, see [Export variables, types, and functions](./bicep-import.md#export-variables-types-and-functions).
143
218
144
-
## Use variables
145
-
146
-
The following example shows how to use the variable for a resource property. You reference the value for the variable by providing the variable's name: `storageName`.
147
-
148
-
```bicep
149
-
param rgLocation string
150
-
param storageNamePrefix string = 'STG'
151
-
152
-
var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'
Because storage account names must use lowercase letters, the `storageName` variable uses the `toLower` function to make the `storageNamePrefix` value lowercase. The `uniqueString` function creates a unique value from the resource group ID. The values are concatenated to a string.
167
-
168
219
## Configuration variables
169
220
170
221
You can define variables that hold related values for configuring an environment. You define the variable as an object with the values. The following example shows an object that holds values for two environments - **test** and **prod**. Pass in one of these values during deployment.
0 commit comments