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
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/15/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 a 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.
@@ -77,6 +77,117 @@ The preceding example returns a value like the following:
77
77
}
78
78
```
79
79
80
+
### Typed variables
81
+
82
+
Bicep supports **typed variables**, where you explicitly declare the data type of a variable to ensure type safety and improve code clarity. By specifying a type, you help the Bicep compiler catch type-related errors during compilation and make the code more maintainable.
83
+
84
+
#### Syntax for typed variables
85
+
86
+
To define a typed variable, use the `var` keyword followed by the variable name, a colon (`:`), the type, and the assigned value.
87
+
88
+
```bicep
89
+
var resourceName: string = 'myResource'
90
+
var instanceCount: int = 3
91
+
var isProduction: bool = true
92
+
var tags: object = { environment: 'dev' }
93
+
var subnets: array = ['subnet1', 'subnet2']
94
+
```
95
+
96
+
#### Supported types
97
+
98
+
Bicep supports the following types for variables:
99
+
100
+
-**Primitive types**:
101
+
-`string`: Text values (e.g., `'hello'`)
102
+
-`int`: Integer values (e.g., `42`)
103
+
-`bool`: Boolean values (`true` or `false`)
104
+
-**Complex types**:
105
+
-`array`: A list of values (e.g., `[1, 2, 3]`)
106
+
-`object`: A key-value collection (e.g., `{ key: 'value' }`)
107
+
-**Union types** (Bicep 0.4 or later):
108
+
- Allows a variable to accept multiple types (e.g., `string | int`).
109
+
- Example:
110
+
111
+
```bicep
112
+
var flexibleId: string | int = 'resource123'
113
+
```
114
+
115
+
- **Literal types**:
116
+
- Restrict a variable to specific literal values (e.g., `'small' | 'medium' | 'large'`).
117
+
- Example:
118
+
119
+
```bicep
120
+
var size: 'small' | 'medium' | 'large' = 'medium'
121
+
```
122
+
123
+
#### Object schemas
124
+
125
+
For `object` types, you can define a schema to enforce a specific structure.
126
+
127
+
```bicep
128
+
var config: {
129
+
name: string
130
+
count: int
131
+
enabled: bool
132
+
} = {
133
+
name: 'myApp'
134
+
count: 5
135
+
enabled: true
136
+
}
137
+
```
138
+
139
+
The compiler ensures the object adheres to the defined schema.
140
+
141
+
#### Benefits of typed variables
142
+
143
+
-**Error detection**: The Bicep compiler validates that assigned values match the declared type, catching errors early.
144
+
-**Code clarity**: Explicit types make it clear what kind of data a variable holds.
145
+
-**Intellisense support**: Tools like Visual Studio Code provide better autocompletion and validation for typed variables.
146
+
-**Refactoring safety**: Ensures that changes to variable assignments don’t inadvertently break type expectations.
147
+
148
+
#### Example with typed variables
149
+
150
+
The following example uses typed variables with decorators to enforce constraints:
151
+
152
+
```bicep
153
+
@description('The environment to deploy to')
154
+
@allowed(['dev', 'test', 'prod'])
155
+
param environment: string = 'dev'
156
+
157
+
var instanceCount: int = environment == 'prod' ? 5 : 2
0 commit comments