Tutorial: Add functions to Azure Resource Manager Bicep file
In this tutorial, you learn how to add template functions to your Bicep file. You use functions to dynamically construct values. Currently, Bicep doesn't support user-defined functions. This tutorial takes 7 minutes to complete.
Note
This article contains Bicep examples. Bicep is currently in preview. For more information, see Project Bicep.
Prerequisites
We recommend that you complete the tutorial about parameters, 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.
Review Bicep file
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'
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: storageName
location: 'chinaeast'
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
The location of the storage account is hard-coded to China East. However, you may need to deploy the storage account to other regions. You're again facing an issue of your Bicep file lacking flexibility. You could add a parameter for location, but it would be great if its default value made more sense than just a hard-coded value.
Use function
Functions add flexibility to your Bicep file by dynamically getting values during deployment. In this tutorial, you use a function to get the location of the resource group you're using for deployment.
The following example shows the changes to add a parameter called location
. The parameter default value calls the resourceGroup function. This function returns an object with information about the resource group being used for deployment. One of the properties on the object is a location property. When you use the default value, the storage account location has the same location as the resource group. The resources inside a resource group don't have to share the same location. You can also provide a different location when needed.
Copy the whole file and replace your Bicep file with its 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
}
}
Deploy Bicep file
In the previous tutorials, you created a storage account in China East, but your resource group was created in China North. For this tutorial, your storage account is created in the same region as the resource group. Use the default value for location, so you don't need to provide that parameter value. You must provide a new name for the storage account because you're creating a storage account in a different location. For example, use store2 as the prefix instead of store1.
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 addlocationparameter `
-ResourceGroupName myResourceGroup `
-TemplateFile $bicepFile `
-storageName "{new-unique-name}"
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.
Verify deployment
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 and has the same location as the resource group.
Clean up resources
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.
Next steps
In this tutorial, you used a function when defining the default value for a parameter. In this tutorial series, you'll continue using functions. By the end of the series, you'll add functions to every section of the Bicep file.