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
+21-46Lines changed: 21 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ 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: 05/15/2025
6
+
ms.date: 05/30/2025
7
7
---
8
8
9
9
# Variables in Bicep
@@ -14,7 +14,7 @@ Resource Manager resolves variables before starting the deployment operations. W
14
14
15
15
## Define variables
16
16
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).
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
18
19
19
### Untyped variables
20
20
@@ -79,59 +79,33 @@ The preceding example returns a value like the following output:
79
79
80
80
### Typed variables
81
81
82
-
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:
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
83
84
84
-**Error detection**: The Bicep compiler validates that assigned values match the declared type, catching errors early.
85
85
-**Code clarity**: Explicit types make it clear what kind of data a variable holds.
86
86
-**Intellisense support**: Tools like Visual Studio Code provide better autocompletion and validation for typed variables.
87
87
-**Refactoring safety**: Ensures that changes to variable assignments don’t inadvertently break type expectations.
88
88
89
-
To define a typed variable, use the `var` keyword followed by the variable name, a colon (`:`), the type, and the assigned value:
89
+
To define a typed variable, use the `var` keyword followed by the variable name, the type, and the assigned value:
90
90
91
91
```bicep
92
-
@<decorator>(<argument>)
93
-
var <variable-name>: <data-type> = <variable-value>
92
+
var <variable-name> <data-type> = <variable-value>
94
93
```
95
94
96
95
The following examples show how to define typed variables:
97
96
98
97
```bicep
99
-
var resourceName: string = 'myResource'
100
-
var instanceCount: int = 3
101
-
var isProduction: bool = true
102
-
var tags: object = { environment: 'dev' }
103
-
var subnets: array = ['subnet1', 'subnet2']
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']
104
103
```
105
104
106
-
Bicep supports the following types for variables:
107
-
108
-
-**Primitive types**:
109
-
-`string`: Text values (for example, `'hello'`)
110
-
-`int`: Integer values (for example, `42`)
111
-
-`bool`: Boolean values (`true` or `false`)
112
-
-**Complex types**:
113
-
-`array`: A list of values (for example, `[1, 2, 3]`)
114
-
-`object`: A key-value collection (for example, `{ key: 'value' }`)
115
-
-**Union types** (Bicep 0.4 or later):
116
-
- Allows a variable to accept multiple types (for example, `string | int`).
117
-
- Example:
118
-
119
-
```bicep
120
-
var flexibleId: string | int = 'resource123'
121
-
```
122
-
123
-
- **Literal types**:
124
-
- Restrict a variable to specific literal values (for example, `'small' | 'medium' | 'large'`).
125
-
- Example:
126
-
127
-
```bicep
128
-
var size: 'small' | 'medium' | 'large' = 'medium'
129
-
```
130
-
131
-
For `object` types, you can define a schema to enforce a specific structure.
105
+
For `object` types, you can define a schema to enforce a specific structure. The compiler ensures the object adheres to the defined schema.
132
106
133
107
```bicep
134
-
var config: {
108
+
var config {
135
109
name: string
136
110
count: int
137
111
enabled: bool
@@ -142,20 +116,21 @@ var config: {
142
116
}
143
117
```
144
118
145
-
The compiler ensures the object adheres to the defined schema.
146
-
147
119
The following example uses typed variables with decorators to enforce constraints:
148
120
149
121
```bicep
150
122
@description('The environment to deploy to')
151
123
@allowed(['dev', 'test', 'prod'])
152
-
param environment: string = 'dev'
124
+
param environment string = 'dev'
153
125
154
-
var instanceCount: int = environment == 'prod' ? 5 : 2
155
-
var resourcePrefix: string = 'app'
156
-
var tags: object = {
126
+
var instanceCount int = environment == 'prod' ? 5 : 2
0 commit comments