教學課程:部署本機 ARM 範本

了解如何從您的本機電腦部署 Azure Resource Manager (ARM 範本) 範本。 完成此教學課程大約需要 8 分鐘

此教學課程是系列中的第一個。 當您完成此課程系列時,您會藉由建立連結的範本將範本模組化、將連結的範本儲存在儲存體帳戶中,並使用 SAS 權杖保護連結的範本,並了解如何建立 DevOps 管線以部署範本。 此系列著重於範本部署。 如果您想要了解範本開發,請參閱初學者教學課程

取得工具

首先,請確定您擁有部署範本所需的工具。

命令列部署

您需要 Azure PowerShell 或 Azure CLI 來部署範本。 如需安裝指示,請參閱:

安裝 Azure PowerShell 或 Azure CLI 之後,請確定您是第一次登入。 如需說明,請參閱登入 - PowerShell登入 - Azure CLI

編輯器 (選擇性)

範本是 JSON 檔案。 若要檢閱/編輯範本,您需要良好的 JSON 編輯器。 我們建議使用含 Resource Manager 工具擴充功能的 Visual Studio Code。 如果您需要安裝這些工具,請參閱快速入門:使用 Visual Studio Code 建立 ARM 範本

檢閱範本

範本會部署儲存體帳戶、App Service 方案和 Web 應用程式。 如果您想要建立範本,則可以瀏覽關於快速入門範本的教學課程。 但這並非完成本教學課程的必要條件。

{
  "$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",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ],
      "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": {
    "storageAccountName": "[format('{0}{1}', parameters('projectName'), uniqueString(resourceGroup().id))]",
    "webAppName": "[format('{0}WebApp', parameters('projectName'))]",
    "appServicePlanName": "[format('{0}Plan', parameters('projectName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-01-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2022-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": "2022-09-01",
      "name": "[variables('webAppName')]",
      "location": "[parameters('location')]",
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]"
      ]
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2023-01-01').primaryEndpoints]"
    }
  }
}

重要

儲存體帳戶名稱必須是唯一的,長度介於 3 到 24 個字元,而且只能使用數字小寫字母。 範例範本的 storageAccountName 變數會將最多 11 個字元的 projectName 參數和 13 個字元的 uniqueString 值合併。

以副檔名. json 將範本的複本儲存到您的本機電腦,例如 azuredeploy.json。 您稍後會在本教學課程中部署此範本。

登入 Azure

若要開始使用 Azure PowerShell/Azure CLI 部署範本,請使用您的 Azure 認證登入。

Connect-AzAccount

如果您有多個 Azure 訂用帳戶,請選取您要使用的訂用帳戶。 以您的訂用帳戶資訊取代 [SubscriptionID/SubscriptionName] 和方括弧 []

Set-AzContext [SubscriptionID/SubscriptionName]

建立資源群組

部署範本時,您必須指定將包含資源的資源群組。 執行部署命令之前,使用 Azure CLI 或 Azure PowerShell 來建立資源群組。 選取下列程式碼區段中的索引標籤,以在 Azure PowerShell 和 Azure CLI 之間進行選擇。 本文中的 CLI 範例是針對 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 "Central US"

部署範本

使用一或兩個部署選項來部署範本。

$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 部署資源

清除資源

您可以藉由刪除資源群組來清除您所部署的資源。

  1. 在 Azure 入口網站中,選取左側功能表中的 [資源群組]
  2. 在 [依名稱篩選] 欄位中輸入資源群組名稱。
  3. 選取資源群組名稱。
  4. 從頂端功能表中選取 [刪除資源群組]

下一步

您已了解如何部署本機範本。 在下一個教學課程中,您會將範本分成主要範本和連結的範本,並了解如何儲存和保護連結的範本。