Skip to content

Commit 2bb21e7

Browse files
committed
Document typed variables
1 parent 145a045 commit 2bb21e7

File tree

1 file changed

+121
-10
lines changed

1 file changed

+121
-10
lines changed

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

Lines changed: 121 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ title: Variables in Bicep
33
description: Describes how to define variables in Bicep
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 04/28/2025
6+
ms.date: 05/15/2025
77
---
88

99
# Variables in Bicep
1010

1111
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.
1212

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).
1614

1715
## Define variables
1816

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:
2022

2123
```bicep
2224
@<decorator>(<argument>)
2325
var <variable-name> = <variable-value>
2426
```
2527

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.
2929

3030
```bicep
31-
var stringVar = 'example value'
31+
var stringVar = 'preset variable'
3232
```
3333

3434
You can use the value from a parameter or another variable when constructing the variable.
@@ -44,7 +44,7 @@ output addToVar string = concatToVar
4444
output addToParam string = concatToParam
4545
```
4646

47-
The preceding example returns:
47+
The output from the preceding example returns:
4848

4949
```json
5050
{
@@ -77,6 +77,117 @@ The preceding example returns a value like the following:
7777
}
7878
```
7979

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
158+
var resourcePrefix: string = 'app'
159+
var tags: object = {
160+
environment: environment
161+
deployedBy: 'bicep'
162+
}
163+
164+
resource storage 'Microsoft.Storage/storageAccounts@2023-01-01' = {
165+
name: '${resourcePrefix}storage${instanceCount}'
166+
location: 'westus'
167+
tags: tags
168+
kind: 'Storage'
169+
sku: {
170+
name: 'Standard_LRS'
171+
}
172+
}
173+
```
174+
175+
In this example:
176+
177+
- `instanceCount` is typed as `int` and uses a conditional expression.
178+
- `resourcePrefix` is typed as `string`.
179+
- `tags` is typed as `object` with a flexible structure.
180+
181+
#### Best practices for typed variables
182+
183+
- **Always specify types**: Explicitly declare types for clarity and to avoid unintended type changes.
184+
- **Use decorators**: Combine typed variables with decorators like `@minValue`, `@maxLength`, or `@allowed` for additional validation.
185+
- **Define object schemas**: For complex objects, use schemas to enforce structure.
186+
- **Keep types simple**: Avoid overly complex union types to maintain readability.
187+
- **Validate with Bicep CLI**: Use `az bicep build` to catch type errors before deployment.
188+
189+
## Use iterative loops
190+
80191
You can use iterative loops when defining a variable. The following example creates an array of objects with three properties.
81192

82193
```bicep

0 commit comments

Comments
 (0)