教程:创建和部署第一个 Azure 资源管理器 Bicep 文件Tutorial: Create and deploy first Azure Resource Manager Bicep file

本教程介绍 BicepThis tutorial introduces you to Bicep. 其中将会说明如何创建初学者 Bicep 文件并将其部署到 Azure。It shows you how to create a starter Bicep file and deploy it to Azure. 本教程还将介绍 Bicep 文件的结构,以及处理 Bicep 文件时所需的工具。You'll learn about the structure of the Bicep file and the tools you'll need for working with Bicep files. 完成本教程大约需要 12 分钟,但实际时间根据需要安装的工具数量而异。It takes about 12 minutes to complete this tutorial, but the actual time will vary based on how many tools you need to install.

本文是相关教学系列的第一篇教程。This tutorial is the first of a series. 在学习该教程系列的过程中,你将逐步修改入门 Bicep 文件,直到了解了 Bicep 文件的所有核心部分。As you progress through the series, you modify the starting Bicep file step by step until you've explored all of the core parts of a Bicep file. 这些元素是更复杂的 Bicep 文件的构建基块。These elements are the building blocks for much more complex Bicep files. 我们希望在本教程系列结束时,你能够自信地创建自己的 Bicep 文件,并准备好使用 Bicep 文件自动完成部署。We hope by the end of the series you're confident creating your own Bicep files and ready to automate your deployments with Bicep files.

若要了解使用 Bicep 所带来的好处,以及为何要使用 Bicep 文件自动完成部署,请参阅 BicepIf you want to learn about the benefits of using Bicep and why you should automate deployment with Bicep files, see Bicep.

如果没有 Azure 订阅,请在开始前创建试用版订阅If you don't have an Azure subscription, create a trial subscription before you begin.

备注

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

获取工具Get tools

首先,请确保已获取创建和部署 Bicep 文件所需的工具。Let's start by making sure you have the tools you need to create and deploy Bicep files. 在本地计算机上安装这些工具。Install these tools on your local machine.

编辑器Editor

若要创建 Bicep 文件,需要一个很好的编辑器。To create Bicep files, you need a good editor. 我们建议使用装有 Bicep 扩展的 Visual Studio Code。We recommend Visual Studio Code with the Bicep extension. 如果需要安装这些工具,请参阅配置 Bicep 开发环境If you need to install these tools, see Configure Bicep development environment.

命令行部署Command-line deployment

您可以使用 Azure CLI 或 Azure PowerShell 来部署 Bicep 文件。You can deploy Bicep files by using Azure CLI or Azure PowerShell. 对于 Azure CLI,需要 2.20.0 或更高版本;对于 Azure PowerShell,需要 5.6.0 或更高版本。For Azure CLI, you need version 2.20.0 or later; for Azure PowerShell, you need version 5.6.0 or later. 有关安装说明,请参阅:For the installation instructions, see:

安装 Azure PowerShell 或 Azure CLI 后,请务必完成首次登录。After installing either Azure PowerShell or Azure CLI, make sure you sign in for the first time. 有关帮助,请参阅登录 - PowerShell登录 - Azure CLIFor help, see Sign in - PowerShell or Sign in - Azure CLI.

好了,现在可以开始了解 Bicep。Okay, you're ready to start learning about Bicep.

创建第一个 Bicep 文件Create your first Bicep file

  1. 打开装有 Bicep 扩展的 Visual Studio Code。Open Visual Studio Code with the Bicep extension installed.

  2. 在“文件”菜单中,选择“新建文件”以创建新的文件。 From the File menu, select New File to create a new file.

  3. 在“文件”菜单中选择“另存为”。 From the File menu, select Save as.

  4. 将文件命名为 azuredeploy,并选择 bicep 文件扩展名。Name the file azuredeploy and select the bicep file extension. 完整的文件名为 azuredeploy.bicepThe complete name of the file is azuredeploy.bicep.

  5. 将该文件保存到工作站。Save the file to your workstation. 选择容易记住的路径,因为稍后在部署 Bicep 文件时需要提供该路径。Select a path that is easy to remember because you'll provide that path later when deploying the Bicep file.

  6. 将以下内容复制并粘贴到该文件中:Copy and paste the following contents into the file:

    resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
      name: '{provide-unique-name}'  // must be globally unique
      location: 'chinaeast'
      sku: {
        name: 'Standard_LRS'
      }
      kind: 'StorageV2'
      properties: {
        supportsHttpsTrafficOnly: true
      }
    }
    

    Visual Studio Code 环境如下所示:Here's what your Visual Studio Code environment looks like:

    Visual Studio Code - 第一个 Bicep 文件

    资源声明具有四个组件:The resource declaration has four components:

    • 资源:关键字。resource: keyword.

    • 符号名称 (stg):符号名称是用于在整个 bicep 文件中引用资源的标识符。symbolic name (stg): A symbolic name is an identifier for referencing the resource throughout your bicep file. 它不是在部署资源时的资源名称。It is not what the name of the resource will be when it's deployed. 资源名称由 name 属性定义。The name of the resource is defined by the name property. 请参阅此列表中的第四个组件。See the fourth component in this list. 为使教程易于理解,在本教程系列中,我们将 stg 用作存储帐户资源的符号名称。To make the tutorials easy to follow, stg is used as the symbolic name for the storage account resource in this tutorial series. 若要查看如何使用符号名称获取对象属性的完整列表,请参阅添加输出To see how to use the symbolic name to get a full list of the object properties, see Add outputs.

    • 资源类型 (Microsoft.Storage/storageAccounts@2019-06-01):它包括资源提供程序 (Microsoft.Storage)、资源类型 (storageAccounts) 和 apiVersion (2019-06-01)。resource type (Microsoft.Storage/storageAccounts@2019-06-01): It is composed of the resource provider (Microsoft.Storage), resource type (storageAccounts), and apiVersion (2019-06-01). 每个资源提供程序都发布了其自身的 API 版本,因此此值与特定的类型相关。Each resource provider publishes its own API versions, so this value is specific to the type.

    • 属性(= {...} 内部的任何内容):要为给定的资源类型指定的特定属性。properties (everything inside = {...}): These are the specific properties you would like to specify for the given resource type. 这些属性与 ARM 模板中提供的属性完全相同。These are exactly the same properties available to you in an ARM Template. 每个资源都有一个 name 属性。Every resource has a name property. 大多数资源还有一个 location 属性,该属性用于设置资源部署到的区域。Most resources also have a location property, which sets the region where the resource is deployed. 其他属性因资源类型和 API 版本而异。The other properties vary by resource type and API version. 了解 API 版本与可用属性之间的关联非常重要,因此,让我们直接了解详情。It's important to understand the connection between the API version and the available properties, so let's jump into more detail.

      对于此存储帐户,可以在 storageAccounts 2019-06-01 中看到 API 版本。For this storage account, you can see that API version at storageAccounts 2019-06-01. 请注意,你并未将所有属性添加到 Bicep 文件。Notice that you didn't add all of the properties to your Bicep file. 许多属性是可选的。Many of the properties are optional. Microsoft.Storage 资源提供程序可能发布了新的 API 版本,但要部署的版本不需要更改。The Microsoft.Storage resource provider could release a new API version, but the version you're deploying doesn't have to change. 可以继续使用该版本,部署的结果是一致的。You can continue using that version and know that the results of your deployment will be consistent.

      如果查看较旧的 API 版本(例如 storageAccounts 2016-05-01),你会发现提供了一个较小的属性集。If you view an older API version, such as storageAccounts 2016-05-01, you'll see that a smaller set of properties is available.

      如果你决定更改资源的 API 版本,请确保评估该版本的属性,并相应地调整 Bicep 文件。If you decide to change the API version for a resource, make sure you evaluate the properties for that version and adjust your Bicep file appropriately.

    有关详细信息,请参阅 Bicep 结构For more information, see Bicep structure.

    名称属性有一个注释。There is a comment for the name property. // 用于单行注释或将 /* ... */ 用于多行注释Use // for single-line comments or /* ... */ for multi-line comments

  7. {provide-unique-name}(包括大括号 {})替换为唯一的存储帐户名称。Replace {provide-unique-name} including the curly braces {} with a unique storage account name.

    重要

    存储帐户名称在 Azure 中必须是唯一的。The storage account name must be unique across Azure. 该名称只能包含小写字母或数字。The name must have only lowercase letters or numbers. 其长度不能超过 24 个字符。It can be no longer than 24 characters. 可以尝试使用某种命名模式,例如,使用 store1 作为前缀,然后添加你的姓名首字母缩写和当天的日期。You might try a naming pattern like using store1 as a prefix and then adding your initials and today's date. 例如,使用的名称类似于 store1abc09092019For example, the name you use could look like store1abc09092019.

    存储帐户的唯一名称不容易猜出,尤其是在大型部署的自动化中不太适合进行这种猜测。Guessing a unique name for a storage account isn't easy and doesn't work well for automating large deployments. 稍后在本教程系列中,你将使用 Bicep 功能来更轻松地创建唯一名称。Later in this tutorial series, you'll use Bicep features that make it easier to create a unique name.

  8. 保存文件。Save the file.

祝贺你,现已创建第一个 Bicep 文件。Congratulations, you've created your first Bicep file.

登录 AzureSign in to Azure

若要开始使用 Azure PowerShell/Azure CLI,请使用你的 Azure 凭据登录。To start working with Azure PowerShell/Azure CLI, sign in with your Azure credentials.

使用以下代码部分中的选项卡在 Azure PowerShell 与 Azure CLI 之间进行选择。Select the tabs in the following code sections to choose between Azure PowerShell and Azure CLI. 本文中的 CLI 示例针对 Bash shell 编写。The CLI examples in this article are written for the Bash shell.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

Connect-AzAccount -Environment AzureChinaCloud

如果你有多个 Azure 订阅,请选择要使用的订阅。If you have multiple Azure subscriptions, select the subscription you want to use. [SubscriptionID/SubscriptionName] 和方括号 [] 替换为你的订阅信息:Replace [SubscriptionID/SubscriptionName] and the square brackets [] with your subscription information:

Set-AzContext [SubscriptionID/SubscriptionName]

创建资源组Create resource group

部署 Bicep 文件时,请指定一个包含资源的资源组。When you deploy a Bicep file, you specify a resource group that will contain the resources. 在运行部署命令之前,请使用 Azure CLI 或 Azure PowerShell 创建该资源组。Before running the deployment command, create the resource group with either Azure CLI or Azure PowerShell.

New-AzResourceGroup `
  -Name myResourceGroup `
  -Location "China North"

部署 Bicep 文件Deploy Bicep file

Bicep 是基于 Azure 资源管理器模板(ARM 模板)的透明抽象。Bicep is a transparent abstraction over Azure Resource Manager templates (ARM templates). 在部署每个 Bicep 文件之前,需将该文件编译成标准的 ARM 模板。Each Bicep file compiles to a standard ARM template before it is deployed. 可以在部署 Bicep 文件之前将其编译成 ARM 模板,也可以直接部署 Bicep 文件。You can either compile your Bicep file into an ARM template before deploying it or directly deploy your Bicep file. 若要部署 Bicep 文件,请使用 Azure CLI 或 Azure PowerShell。To deploy the Bicep file, use either Azure CLI or Azure PowerShell. 使用创建的资源组。Use the resource group you created. 为部署指定一个适当的名称,以便可以在部署历史记录中轻松识别该部署。Give a name to the deployment so you can easily identify it in the deployment history. 为方便起见,另请创建一个用于存储 Bicep 文件路径的变量。For convenience, also create a variable that stores the path to the Bicep file. 此变量使你能够更轻松地运行部署命令,因为无需在每次部署时都重新键入路径。This variable makes it easier for you to run the deployment commands because you don't have to retype the path every time you deploy. {provide-the-path-to-the-bicep-file}(包括大括号 {})替换成扩展名为 .bicep 的 Bicep 文件的路径。Replace {provide-the-path-to-the-bicep-file} including the curly braces {} with the path to your Bicep file with the .bicep file extension name.

若要运行此部署 cmdlet,你必须具有 Azure PowerShell 的最新版本To run this deployment cmdlet, you must have the latest version of Azure PowerShell.

$bicepFile = "{provide-the-path-to-the-bicep-file}"
New-AzResourceGroupDeployment `
  -Name firstbicep `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $bicepFile

部署命令将返回结果。The deployment command returns results. 查看 ProvisioningState 以确定部署是否成功。Look for ProvisioningState to see whether the deployment succeeded.

备注

如果部署失败,请使用 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 deploy in the last procedure. 默认名称为 myResourceGroupThe default name is myResourceGroup. 你应会看到,该资源组中未部署任何资源。You shall see no resource deployed within the resource group.

  4. 请注意,在概述的右上角显示了部署状态。Notice in the upper right of the overview, the status of the deployment is displayed. 选择“1 成功”。Select 1 Succeeded.

    查看部署状态

  5. 将会看到该资源组的部署历史记录。You see a history of deployment for the resource group. 选择“firstbicep”。Select firstbicep.

    选择部署

  6. 将会看到部署摘要。You see a summary of the deployment. 已部署了一个存储帐户。There is one storage account deployed. 请注意,在左侧可以查看部署过程中使用的输入、输出和模板。Notice on the left you can view inputs, outputs, and the template used during deployment.

    查看部署摘要

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

现已创建一个要部署到 Azure 的简单 Bicep 文件。You created a simple Bicep file to deploy to Azure. 后续教程会介绍如何将参数、变量、输出和模块添加到 Bicep 文件。In the later tutorials, you learn how to add parameters, variables, outputs, and modules to a Bicep file. 这些功能是更复杂的 Bicep 文件的构建基块。These features are the building blocks for much more complex Bicep files.