Variables in Bicep

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 are limited to 256 variables in a Bicep file. For more information, see Template limits.

Define variable

The syntax for defining a variable is:

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.

var stringVar = 'example value'

You can use the value from a parameter or another variable when constructing the variable.

param inputValue string = 'deployment parameter'

var stringVar = 'preset variable'
var concatToVar =  '${stringVar}AddToVar'
var concatToParam = '${inputValue}AddToParam'

output addToVar string = concatToVar
output addToParam string = concatToParam

The preceding example returns:

{
  "addToParam": {
    "type": "String",
    "value": "deployment parameterAddToParam"
  },
  "addToVar": {
    "type": "String",
    "value": "preset variableAddToVar"
  }
}

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.

param storageNamePrefix string = 'stg'
var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'

output uniqueStorageName string = storageName

The preceding example returns a value like the following:

"uniqueStorageName": {
  "type": "String",
  "value": "stghzuunrvapn6sw"
}

You can use iterative loops when defining a variable. The following example creates an array of objects with three properties.

param itemCount int = 3

var objectArray = [for i in range(0, itemCount): {
  name: 'myDataDisk${(i + 1)}'
  diskSizeGB: '1'
  diskIndex: i
}]

output arrayResult array = objectArray

The output returns an array with the following values:

[
  {
    "name": "myDataDisk1",
    "diskSizeGB": "1",
    "diskIndex": 0
  },
  {
    "name": "myDataDisk2",
    "diskSizeGB": "1",
    "diskIndex": 1
  },
  {
    "name": "myDataDisk3",
    "diskSizeGB": "1",
    "diskIndex": 2
  }
]

For more information about the types of loops you can use with variables, see Iterative loops in Bicep.

Use variable

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.

param rgLocation string
param storageNamePrefix string = 'STG'

var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'

resource demoAccount 'Microsoft.Storage/storageAccounts@2021-02-01' = {
  name: storageName
  location: rgLocation
  kind: 'Storage'
  sku: {
    name: 'Standard_LRS'
  }
}

output stgOutput string = 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.

@allowed([
  'test'
  'prod'
])
param environmentName string

var environmentSettings = {
  test: {
    instanceSize: 'Small'
    instanceCount: 1
  }
  prod: {
    instanceSize: 'Large'
    instanceCount: 4
  }
}

output instanceSize string = environmentSettings[environmentName].instanceSize
output instanceCount int = environmentSettings[environmentName].instanceCount

Next steps