Skip to content

Commit c50773a

Browse files
Merge pull request #299911 from mumian/0515-typed-variables
Document Bicep typed variables
2 parents b1fe780 + 430f025 commit c50773a

File tree

1 file changed

+86
-35
lines changed

1 file changed

+86
-35
lines changed

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

Lines changed: 86 additions & 35 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/30/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 an 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
{
@@ -68,7 +68,7 @@ var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().i
6868
output uniqueStorageName string = storageName
6969
```
7070

71-
The preceding example returns a value like the following:
71+
The preceding example returns a value like the following output:
7272

7373
```json
7474
"uniqueStorageName": {
@@ -77,6 +77,81 @@ The preceding example returns a value like the following:
7777
}
7878
```
7979

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
127+
var resourcePrefix string = 'app'
128+
var tags {
129+
environment: string
130+
deployedBy: string
131+
} = {
132+
environment: environment
133+
deployedBy: 'Bicep'
134+
}
135+
136+
resource storage 'Microsoft.Storage/storageAccounts@2023-01-01' = {
137+
name: '${resourcePrefix}storage${instanceCount}'
138+
location: 'westus'
139+
tags: tags
140+
kind: 'Storage'
141+
sku: {
142+
name: 'Standard_LRS'
143+
}
144+
}
145+
```
146+
147+
In this example:
148+
149+
- `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+
80155
You can use iterative loops when defining a variable. The following example creates an array of objects with three properties.
81156

82157
```bicep
@@ -141,30 +216,6 @@ Markdown-formatted text can be used for the description text.
141216

142217
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).
143218

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)}'
153-
154-
resource demoAccount 'Microsoft.Storage/storageAccounts@2024-01-01' = {
155-
name: storageName
156-
location: rgLocation
157-
kind: 'Storage'
158-
sku: {
159-
name: 'Standard_LRS'
160-
}
161-
}
162-
163-
output stgOutput string = storageName
164-
```
165-
166-
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-
168219
## Configuration variables
169220

170221
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

Comments
 (0)