教程:使用 Azure 快速入门模板Tutorial: Use Azure Quickstart templates

Azure 快速入门模板是一个存储库,其中包含社区贡献的模板。Azure Quickstart templates is a repository of community contributed templates. 可以在模板开发中使用示例模板。You can use the sample templates in your template development. 在本教程中,我们需找到一个网站资源定义,然后将其添加到自己的模板中。In this tutorial, you find a website resource definition, and add it to your own template. 完成该过程需要大约 12 分钟It takes about 12 minutes to complete.

先决条件Prerequisites

建议完成有关已导出模板的教程,但这不是必需的。We recommend that you complete the tutorial about exported templates, but it's not required.

必须已安装带有资源管理器工具扩展的 Visual Studio Code,以及 Azure PowerShell 或 Azure CLI。You must have Visual Studio Code with the Resource Manager Tools extension, and either Azure PowerShell or Azure CLI. 有关详细信息,请参阅模板工具For more information, see template tools.

审阅模板Review template

在上一篇教程的结束时,模板包含以下 JSON:At the end of the previous tutorial, your template had the following JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2016-09-01",
      "name": "[parameters('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
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

此模板适用于部署存储帐户和应用服务计划,但你可能需要向其添加网站。This template 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.

查找模板Find template

  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.

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

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

修订现有模板Revise existing template

将快速入门模板与现有模板合并:Merge the quickstart template with the existing template:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ]
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    },
    "webAppName": {
      "type": "string",
      "metadata": {
        "description": "Base name of the resource such as web app name and app service plan "
      },
      "minLength": 2
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "php|7.0",
      "metadata": {
        "description": "The Runtime stack of current web app"
      }
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]",
    "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2016-09-01",
      "name": "[parameters('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": "2018-11-01",
      "name": "[variables('webAppPortalName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
      ],
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
        "siteConfig": {
            "linuxFxVersion": "[parameters('linuxFxVersion')]"
          }
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

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

Microsoft.Web/serverfarms 定义末尾添加一个逗号,以便将资源定义与 Microsoft.Web/sites 定义分开。Add a comma at the end of the Microsoft.Web/serverfarms definition to separate the resource definition from the Microsoft.Web/sites definition.

在这个新资源中,有一些需要注意的重要功能。There are a couple of important features to note in this new resource.

你会注意到,它有一个名为 dependsOn 的元素,该元素设置为应用服务计划。You'll notice it has an element named dependsOn that's set to the app service plan. 此设置是必需的,因为在创建 Web 应用之前,必须存在应用服务计划。This setting is required because the app service plan must exist before the web app is created. dependsOn 元素告知资源管理器如何将用于部署的资源排序。The dependsOn element tells Resource Manager how to order the resources for deployment.

serverFarmId 属性使用 resourceId 函数。The serverFarmId property uses the resourceId function. 此函数获取资源的唯一标识符。This function gets the unique identifier for a resource. 在此示例中,它获取应用服务计划的唯一标识符。In this case, it gets the unique identifier for the app service plan. Web 应用与一个特定的应用服务计划相关联。The web app is associated with one specific app service plan.

部署模板Deploy template

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

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

New-AzResourceGroupDeployment `
  -Name addwebapp `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -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

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