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.
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.
Define variables
The syntax for defining a variable is:
Bicep
@<decorator>(<argument>)
var <variable-name> = <variable-value>
A variable can't have the same name as a parameter, module, or resource.
Notice that you don't specify a data type for the variable. The type is inferred from the value. The following example sets a variable to a string.
Bicep
varstringVar = 'example value'
You can use the value from a parameter or another variable when constructing the variable.
You can use Bicep functions to construct the variable value. The following example uses Bicep functions to create a string value for a storage account name.
For more information about the types of loops you can use with variables, see Iterative loops in Bicep.
Use decorators
Decorators are written in the format @expression and are placed above variable declarations. The following table shows the available decorators for variables.
Indicates that the variable is available for import by another Bicep file.
Decorators are in the sys namespace. If you need to differentiate a decorator from another item with the same name, preface the decorator with sys. For example, if your Bicep file includes a variable named description, you must add the sys namespace when using the description decorator.
Description
To add explanation, add a description to variable declaration. For example:
Bicep
@description('Create a unique storage account name.')varstorageAccountName = uniqueString(resourceGroup().id)
Markdown-formatted text can be used for the description text.
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.
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.
Configuration variables
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.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.