Tutorial: Add variables to Azure Resource Manager Bicep file
In this tutorial, you learn how to add a variable to your Bicep file. Variables simplify your Bicep files by enabling you to write an expression once and reuse it throughout the Bicep file. This tutorial takes 7 minutes to complete.
Note
This article contains Bicep examples. Bicep is currently in preview. For more information, see Project Bicep.
We recommend that you complete the tutorial about functions, but it's not required.
You must have Visual Studio Code with the Bicep extension, and either Azure PowerShell or Azure CLI. For more information, see Bicep tools.
At the end of the previous tutorial, your Bicep file had the following contents:
@minLength(3)
@maxLength(24)
param storageName string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: storageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
The parameter for the storage account name is hard-to-use because you have to provide a unique name. If you've completed the earlier tutorials in this series, you're probably tired of guessing a unique name. You solve this problem by adding a variable that constructs a unique name for the storage account.
The following example shows the changes to add a variable to your Bicep file that creates a unique storage account name. Copy the whole file and replace your Bicep file with its contents.
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: uniqueStorageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
Notice that it includes a variable named uniqueStorageName
. This variable uses three functions to construct a string value.
You're familiar with the resourceGroup function. In this case, you get the id
property instead of the location
property, as shown in the previous tutorial. The id
property returns the full identifier of the resource group, including the subscription ID and resource group name.
The uniqueString function creates a 13 character hash value. The returned value is determined by the parameters you pass in. For this tutorial, you use the resource group ID as the input for the hash value. That means you could deploy this Bicep file to different resource groups and get a different unique string value. However, you get the same value if you deploy to the same resource group.
Bicep supports a string interpolation syntax. For this variable, it takes the string from the parameter and the string from the uniqueString
function, and combines them into one string.
The storagePrefix
parameter enables you to pass in a prefix that helps you identify storage accounts. You can create your own naming convention that makes it easier to identify storage accounts after deployment from a long list of resources.
Finally, notice that the storage name is now set to the variable instead of a parameter.
Let's deploy the Bicep file. Deploying this Bicep file is easier than the previous Bicep files because you provide just the prefix for the storage name.
If you haven't created the resource group, see Create resource group. The example assumes you've set the bicepFile
variable to the path to the Bicep file, as shown in the first tutorial.
To run this deployment cmdlet, you must have the latest version of Azure PowerShell.
New-AzResourceGroupDeployment `
-Name addnamevariable `
-ResourceGroupName myResourceGroup `
-TemplateFile $bicepFile `
-storagePrefix "store" `
-storageSKU Standard_LRS
Note
If the deployment failed, use the verbose
switch to get information about the resources being created. Use the debug
switch to get more information for debugging.
You can verify the deployment by exploring the resource group from the Azure portal.
- Sign in to the Azure portal.
- From the left menu, select Resource groups.
- Select the resource group you deployed to.
- You see that a storage account resource has been deployed. The name of the storage account is store plus a string of random characters.
If you're moving on to the next tutorial, you don't need to delete the resource group.
If you're stopping now, you might want to clean up the resources you deployed by deleting the resource group.
- From the Azure portal, select Resource group from the left menu.
- Enter the resource group name in the Filter by name field.
- Select the resource group name.
- Select Delete resource group from the top menu.
In this tutorial, you added a variable that creates a unique name for a storage account. In the next tutorial, you return a value from the deployed storage account.