教程:使用 Azure 快速入门模板进行 Azure 资源管理器 Bicep 开发Tutorial: Use Azure Quickstart templates for Azure Resource Manager Bicep development

Azure 快速入门模板是一个存储库,其中包含社区贡献的 JSON 模板。Azure Quickstart templates is a repository of community contributed JSON templates. 可以在 Bicep 开发中使用示例模板。You can use the sample templates in your Bicep development. 在本教程中,你将查找网站资源定义,将其反向编译为 Bicep,然后将其添加到 Bicep 文件。In this tutorial, you find a website resource definition, decompile it to Bicep, and add it to your Bicep file. 完成该过程需要大约 12 分钟It takes about 12 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 exported templates, 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(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location
param appServicePlanName string = 'exampleplan'

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
  }
}

resource appPlan 'Microsoft.Web/serverfarms@2016-09-01' = {
  name: appServicePlanName
  location: location
  sku: {
    name: 'B1'
    tier: 'Basic'
    size: 'B1'
    family: 'B'
    capacity: 1
  }
  kind: 'linux'
  properties: {
    perSiteScaling: false
    reserved: true
    targetWorkerCount: 0
    targetWorkerSizeId: 0
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

此 Bicep 文件适用于部署存储帐户和应用服务计划,但你可能想要在其中添加网站。This Bicep file works for deploying storage accounts and app service plans, but you might want to add a website to it. 可以使用预生成的模板来快速发现部署资源所需的 JSON。You can use pre-built templates to quickly discover the JSON required for deploying a resource. 在 Azure 快速入门模板提供 Bicep 示例之前,你可以使用转换工具将 JSON 模板转换为 Bicep 文件。Before Azure Quickstart templates offer Bicep samples, you can use conversion tools to convert JSON templates to Bicep files.

查找模板Find template

Azure 快速入门模板目前仅提供 JSON 模板。Currently, the Azure Quickstart templates only provide JSON templates. 可使用一些工具将 JSON 模板反向编译为 Bicep 模板。There are tools you can use to decompile JSON templates to Bicep templates.

  1. 打开 Azure 快速入门模板Open Azure Quickstart templates

  2. 在“搜索”中,输入“部署 linux web 应用”。 In Search, enter deploy linux web app.

  3. 选择标题为“部署基本的 linux web 应用”的磁贴。Select the tile with the title Deploy a basic Linux web app. 如果找不到它,请单击此处的直接链接If you have trouble finding it, here's the direct link.

  4. 选择“在 GitHub 上浏览”。 Select Browse on GitHub.

  5. 选择“azuredeploy.json”。 Select azuredeploy.json. 这是可供使用的模板。This is the template you can use.

  6. 选择“原始”,然后创建 URL 的副本。Select Raw, and then make a copy of the URL.

  7. 浏览到 https://bicepdemo.z22.web.core.windows.net/ ,选择“反向编译”,然后提供原始模板 URL。Browse to https://bicepdemo.z22.web.core.windows.net/, Select Decompile, and then provide the raw template URL.

  8. 查看 Bicep 模板。Review the Bicep template. 具体说来,请查找 Microsoft.Web/sites 资源。In particular, look for the Microsoft.Web/sites resource.

    资源管理器模板快速入门网站

    如果你已使用过 JSON 模板,此新资源中有几个重要的 Bicep 功能值得注意。There are a couple of important Bicep features to note in this new resource if you have worked on JSON templates.

    在 ARM JSON 模板中,必须手动使用 dependsOn 属性指定资源依赖项。In ARM JSON templates, you must manually specify resource dependencies with the dependsOn property. 网站资源依赖于应用服务计划资源。The website resource depends on the app service plan resource. 使用 Bicep 时,如果使用符号名称引用资源的任何属性,则会自动添加 dependsOn 属性。With Bicep, if you reference any property of the resource by using the symbolic name, the dependsOn property is automatically added.

    可以轻松引用应用服务计划符号名称中的资源 ID (appServicePlanName.id),该 ID 将转换为编译的 JSON 模板中的 resourceId(...) 函数。You can easily reference the resource ID from the symbolic name of the app service plan (appServicePlanName.id) which will be translated to the resourceId(...) function in the compiled JSON template.

修改现有的 Bicep 文件Revise existing Bicep file

将反向编译的快速入门模板与现有的 Bicep 文件合并。Merge the decompiled quickstart template with the existing Bicep file. 就像在上一篇教程中所做的那样,请更新资源符号名称和资源名称,使之符合命名约定。Same as what you did in the previous tutorial, update the resource symbolic name, and the resource name to match your naming convention.

@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
param appServicePlanName string = 'exampleplan'

@minLength(2)
@description('Base name of the resource such as web app name and app service plan.')
param webAppName string

@description('The Runtime stack of current web app.')
param linuxFxVersion string = 'php|7.0'

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
var webAppPortalName = '${webAppName}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

resource appPlan 'Microsoft.Web/serverfarms@2016-09-01' = {
  name: appServicePlanName
  location: location
  sku: {
    name: 'B1'
    tier: 'Basic'
    size: 'B1'
    family: 'B'
    capacity: 1
  }
  kind: 'linux'
  properties: {
    perSiteScaling: false
    reserved: true
    targetWorkerCount: 0
    targetWorkerSizeId: 0
  }
}

resource site 'Microsoft.Web/sites@2020-06-01' = {
  name: webAppPortalName
  location: location
  kind: 'app'
  properties: {
    serverFarmId: appPlan.id
    siteConfig: {
      linuxFxVersion: linuxFxVersion
    }
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

Web 应用名称必须在 Azure 中独一无二。The web app name needs to be unique across Azure. 为了防止出现重复名称,我们已将 webAppPortalName 变量从 var webAppPortalName_var = '${webAppName}-webapp' 更新为 var webAppPortalName = '${webAppName}${uniqueString(resourceGroup().id)}'To prevent having duplicate names, the webAppPortalName variable has been updated from var webAppPortalName_var = '${webAppName}-webapp' to var webAppPortalName = '${webAppName}${uniqueString(resourceGroup().id)}'.

部署 Bicep 文件Deploy Bicep file

使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 模板。Use either Azure CLI or Azure PowerShell to deploy a Bicep template.

如果尚未创建资源组,请参阅创建资源组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 addwebapp `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $bicepFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS `
  -webAppName demoapp

备注

如果部署失败,请使用 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.

清理资源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

你已了解如何使用快速入门模板进行 Bicep 开发。You learned how to use a quickstart template for your Bicep development. 在下一教程中,我们向资源添加标记。In the next tutorial, you add tags to the resources.