教程:部署本地 ARM 模板Tutorial: Deploy a local ARM template

了解如何从本地计算机部署 Azure 资源管理器模板(ARM 模板)。Learn how to deploy an Azure Resource Manager template (ARM template) from your local machine. 完成本教程大约需要 8 分钟It takes about 8 minutes to complete.

本文是相关教学系列的第一篇教程。This tutorial is the first of a series. 在学习该系列时,你将通过创建链接模板来将模板模块化,将链接模板存储在存储帐户中,使用 SAS 令牌保护链接模板,并了解如何创建 DevOps 管道来部署模板。As you progress through the series, you modularize the template by creating a linked template, you store the linked template in a storage account, and secure the linked template by using SAS token, and you learn how to create a DevOps pipeline to deploy templates. 该系列重点介绍模板部署。This series focuses on template deployment. 如果希望了解模板开发,请参阅初学者教程If you want to learn template development, see the beginner tutorials.

获取工具Get tools

首先,请确保已获取部署模板所需的工具。Let's start by making sure you have the tools you need to deploy templates.

命令行部署Command-line deployment

你需要使用 Azure PowerShell 或 Azure CLI 来部署模板。You need either Azure PowerShell or Azure CLI to deploy the template. 有关安装说明,请参阅: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.

编辑器(可选)Editor (Optional)

模板是一些 JSON 文件。Templates are JSON files. 若要查看/编辑模板,需要一个好用的 JSON 编辑器。To review/edit templates, you need a good JSON editor. 我们建议使用带有资源管理器工具扩展的 Visual Studio Code。We recommend Visual Studio Code with the Resource Manager Tools extension. 如果需要安装这些工具,请参阅快速入门:使用 Visual Studio Code 创建 ARM 模板If you need to install these tools, see Quickstart: Create ARM templates with Visual Studio Code.

审阅模板Review template

模板部署一个存储帐户、应用服务计划和 Web 应用。The template deploys a storage account, app service plan, and web app. 如果对创建模板感兴趣,则可完成关于快速入门模板的教程If you're interested in creating the template, you can go through tutorial about Quickstart templates. 但这不是完成本教程所必需的。However it's not required for completing this tutorial.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "projectName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11,
      "metadata": {
        "description": "Specify a project name that is used to generate resource names."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specify a location for the resources."
      }
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS",
      ],
      "metadata": {
        "description": "Specify the storage account type."
      }
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "php|7.0",
      "metadata": {
        "description": "Specify the Runtime stack of current web app"
      }
    }
  },
  "variables": {
    "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
    "storageAccountName": "[concat(parameters('projectName'), uniqueString(resourceGroup().id))]",
    "webAppName": "[concat(parameters('projectName'), 'WebApp')]",
    "appServicePlanName": "[concat(parameters('projectName'), 'Plan')]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2020-09-01",
      "name": "[variables('appServicePlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "B1",
        "tier": "Basic",
        "size": "B1",
        "family": "B",
        "capacity": 1
      },
      "kind": "linux",
      "properties": {
        "perSiteScaling": false,
        "reserved": true,
        "targetWorkerCount": 0,
        "targetWorkerSizeId": 0
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2020-09-01",
      "name": "[variables('webAppName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
      ],
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]"
        }
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('storageAccountName')).primaryEndpoints]"
    }
  }
}

重要

存储帐户名称长度必须为 3 到 24 个字符,并且只能使用数字和小写字母。Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only. 该名称必须是唯一的。The name must be unique. 在模板中,存储帐户名称是追加了“store”的项目名称,项目名称的长度必须介于 3 到 11 个字符之间。In the template, the storage account name is the project name with store appended, and the project name must be between 3 and 11 characters. 因此,项目名称必须符合存储帐户名称要求,且短于 11 个字符。So the project name must meet the storage account name requirements and has less than 11 characters.

以 .json 为扩展名将模板的副本保存到本地计算机,例如 azuredeploy.json 。Save a copy of the template to your local computer with the .json extension, for example, azuredeploy.json. 稍后在本教程中将部署此模板。You deploy this template later in the tutorial.

登录 AzureSign in to Azure

若要开始使用 Azure PowerShell/Azure CLI 来部署模板,请使用你的 Azure 凭据登录。To start working with Azure PowerShell/Azure CLI to deploy a template, sign in with your Azure credentials.

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

部署模板时,请指定一个包含资源的资源组。When you deploy a template, 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. 使用以下代码部分中的选项卡在 Azure PowerShell 与 Azure CLI 之间进行选择。Select the tabs in the following code section to choose between Azure PowerShell and Azure CLI. 本文中的 CLI 示例针对 Bash shell 编写。The CLI examples in this article are written for the Bash shell.

$projectName = Read-Host -Prompt "Enter a project name that is used to generate resource and resource group names"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroup `
  -Name $resourceGroupName `
  -Location "China North"

部署模板Deploy template

使用一个或两个部署选项来部署模板。Use one or both deployment options to deploy the template.

$projectName = Read-Host -Prompt "Enter the same project name"
$templateFile = Read-Host -Prompt "Enter the template file path and file name"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroupDeployment `
  -Name DeployLocalTemplate `
  -ResourceGroupName $resourceGroupName `
  -TemplateFile $templateFile `
  -projectName $projectName `
  -verbose

若要详细了解如何使用 Azure PowerShell 部署模板,请参阅使用 ARM 模板和 Azure PowerShell 部署资源To learn more about deploying template by using Azure PowerShell, see Deploy resources with ARM templates and Azure PowerShell.

清理资源Clean up resources

通过删除资源组来清理你部署的资源。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

现在,你已了解了如何部署本地模板。You learned how to deploy a local template. 在下一教程中,你需要将该模板拆分为一个主模板和一个链接模板,并了解如何存储和保护链接模板。In the next tutorial, you separate the template into a main template and a linked template, and learn how to store and secure the linked template.