教程:将函数添加到 Azure 资源管理器 Bicep 文件Tutorial: Add functions to Azure Resource Manager Bicep file

本教程介绍如何将模板函数添加到 Bicep 模板。In this tutorial, you learn how to add template functions to your Bicep file. 我们使用函数来动态构造值。You use functions to dynamically construct values. 目前,Bicep 不支持用户定义的函数。Currently, Bicep doesn't support user-defined functions. 完成本教程需要 7 分钟This tutorial takes 7 minutes to complete.

备注

本文包含 Bicep 示例。This article contains Bicep examples. Bicep 目前为预览版。Bicep is currently in preview. 有关详细信息,请参阅 Bicep 项目For more information, see Project Bicep.

先决条件Prerequisites

建议完成有关参数的教程,但这不是必需的。We recommend that you complete the tutorial about parameters, but it's not required.

必须具有包含 Bicep 扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Bicep extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅 Bicep 工具For more information, see Bicep tools.

查看 Bicep 文件Review Bicep file

在上一篇教程结束时,Bicep 文件包含以下内容: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. 你会再次面对一个问题:Bicep 文件缺乏灵活性。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

函数可以在部署过程中动态获取值,为 Bicep 文件增加了灵活性。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.

以下示例演示了添加名为 location 的参数而需要做出的更改。The following example shows the changes to add a parameter called location. 参数默认值调用 resourceGroup 函数。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.

请复制整个文件,并将你的 Bicep 文件替换为该文件的内容。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
  }
}

部署 Bicep 文件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. 例如,使用 store2 而不是 store1 作为前缀。For example, use store2 as the prefix instead of store1.

如果尚未创建资源组,请参阅创建资源组If you haven't created the resource group, see Create resource group. 此示例假定你已按第一篇教程所述将 bicepFile 变量设置为 Bicep 文件的路径。The example assumes you've set the bicepFile variable to the path to the Bicep file, as shown in the first tutorial.

若要运行此部署 cmdlet,你必须具有 Azure PowerShell 的最新版本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}"

备注

如果部署失败,请使用 verbose 开关获取有关正在创建的资源的信息。If the deployment failed, use the verbose switch to get information about the resources being created. 使用 debug 开关获取调试的详细信息。Use the debug switch to get more information for debugging.

验证部署Verify deployment

可以通过在 Azure 门户中浏览资源组来验证部署。You can verify the deployment by exploring the resource group from the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 在左侧菜单中选择“资源组”。 From the left menu, select Resource groups.
  3. 选择已部署到的资源组。Select the resource group you deployed to.
  4. 可以看到一项存储帐户资源已部署,且其位置与资源组一样。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.

  1. 在 Azure 门户上的左侧菜单中选择“资源组” 。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name.
  4. 在顶部菜单中选择“删除资源组”。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. 到本系列完成时,我们会将函数添加到 Bicep 文件的每个部分。By the end of the series, you'll add functions to every section of the Bicep file.