教學課程:使用參數檔案部署您的 ARM 範本

在此教學課程中,您將了解如何使用參數檔案來儲存您在部署期間傳入的值。 在先前的教學課程中,您已將內嵌參數與部署命令搭配使用。 這種方法可用來測試 Azure Resource Manager 範本 (ARM 範本),但在將部署自動化時,可以更輕易地為您的環境傳遞一組值。 參數檔案可讓您更輕鬆地為特定環境封裝參數值。 在此教學課程中,您將建立適用於開發和實際執行環境的參數檔案。 完成此指示需要 12 分鐘的時間。

必要條件

我們建議您完成有關標記的教學課程,但並非必要。

您必須具有 Visual Studio Code 並搭配 Resource Manager 工具延伸模組,以及 Azure PowerShell 或 Azure 命令列介面 (CLI)。 如需詳細資訊,請參閱範本工具

檢閱範本

範本有許多您可在部署期間提供的參數。 在上一個教學課程結束時,您的範本會具有下列 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",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "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"
      }
    },
    "resourceTags": {
      "type": "object",
      "defaultValue": {
        "Environment": "Dev",
        "Project": "Tutorial"
      }
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]",
    "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[variables('uniqueStorageName')]",
      "location": "[parameters('location')]",
      "tags": "[parameters('resourceTags')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-01",
      "name": "[parameters('appServicePlanName')]",
      "location": "[parameters('location')]",
      "tags": "[parameters('resourceTags')]",
      "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": "2021-03-01",
      "name": "[variables('webAppPortalName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[parameters('appServicePlanName')]"
      ],
      "tags": "[parameters('resourceTags')]",
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]"
        }
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

此範本運作良好,但現在您想要輕鬆地管理您為範本傳入的參數。

新增參數檔案

參數檔案是 JSON 檔案,其結構類似於您的範本。 在檔案中,您會提供想要在部署期間傳入的參數值。

在參數檔案中,您可以在範本中提供參數的值。 參數檔案中每個參數的名稱都必須符合範本中參數的名稱。 名稱不區分大小寫,但若要輕鬆查看相符的值,建議您比對範本中的大小寫。

您不需要為每個參數提供值。 如果未指定的參數具有預設值,則會在部署期間使用該值。 如果參數沒有預設值,而且未在參數檔案中指定,系統就會提示您在部署期間提供值。

您不能在參數檔案中指定與範本中參數名稱不相符的參數名稱。 提供未知的參數時,您會收到錯誤。

在 Visual Studio Code 中,建立含有下列內容的新檔案。 以 azuredeploy.parameters.dev.json 名稱來儲存檔案。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "devstore"
    },
    "storageSKU": {
      "value": "Standard_LRS"
    },
    "appServicePlanName": {
      "value": "devplan"
    },
    "webAppName": {
      "value": "devapp"
    },
    "resourceTags": {
      "value": {
        "Environment": "Dev",
        "Project": "Tutorial"
      }
    }
  }
}

此檔案是適用於開發環境的參數檔案。 請注意,該檔案會針對儲存體帳戶使用 Standard_LRS、使用 dev 前置詞來為資源命名,並將 Environment 標記設定為 Dev

同樣地,建立含有下列內容的新檔案。 以 azuredeploy.parameters.prod.json 名稱來儲存檔案。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "value": "contosodata"
    },
    "storageSKU": {
      "value": "Standard_GRS"
    },
    "appServicePlanName": {
      "value": "contosoplan"
    },
    "webAppName": {
      "value": "contosowebapp"
    },
    "resourceTags": {
      "value": {
        "Environment": "Production",
        "Project": "Tutorial"
      }
    }
  }
}

此檔案是適用於實際執行環境的參數檔案。 請注意,該檔案會針對儲存體帳戶使用 Standard_GRS、使用 contoso 前置詞來為資源命名,並將 Environment 標記設定為 Production。 在真正的實際執行環境中,您也想要將應用程式服務與免費的 SKU 搭配使用,但我們將使用該 SKU 來進行此教學課程。

部署範本

使用 Azure CLI 或 Azure PowerShell 來部署範本。

作為範本的最終測試,讓我們建立兩個新的資源群組。 一個用於開發環境,另一個用於實際執行環境。

針對範本和參數變數,將 {path-to-the-template-file}{path-to-azuredeploy.parameters.dev.json}{path-to-azuredeploy.parameters.prod.json} 和大括弧 {} 取代為您的範本和參數檔案路徑。

首先,我們將部署至開發環境。

$templateFile = "{path-to-the-template-file}"
$parameterFile="{path-to-azuredeploy.parameters.dev.json}"
New-AzResourceGroup `
  -Name myResourceGroupDev `
  -Location "East US"
New-AzResourceGroupDeployment `
  -Name devenvironment `
  -ResourceGroupName myResourceGroupDev `
  -TemplateFile $templateFile `
  -TemplateParameterFile $parameterFile

現在,我們將部署至實際執行環境。

$parameterFile="{path-to-azuredeploy.parameters.prod.json}"
New-AzResourceGroup `
  -Name myResourceGroupProd `
  -Location "West US"
New-AzResourceGroupDeployment `
  -Name prodenvironment `
  -ResourceGroupName myResourceGroupProd `
  -TemplateFile $templateFile `
  -TemplateParameterFile $parameterFile

注意

如果部署失敗,請使用 verbose 參數來取得所建立資源的相關資訊。 使用 debug 參數來取得更多資訊以進行偵錯。

驗證部署

您可以從 Azure 入口網站探索資源群組,藉以確認部署。

  1. 登入 Azure 入口網站
  2. 從左側功能表中,選取 [資源群組]
  3. 您會看到兩個您在此教學課程中部署的新資源群組。
  4. 選取任一個資源群組,並檢視已部署的資源。 請注意,它們會符合您在該環境之參數檔案中指定的值。

清除資源

  1. 在 Azure 入口網站中,選取左側功能表中的 [資源群組]

  2. 選取核取方塊旁邊的超連結資源群組名稱。 如果您已完成本系列,您會有三個要刪除的資源群組:myResourceGroupmyResourceGroupDevmyResourceGroupProd

  3. 從頂端功能表中選取 [刪除資源群組] 圖示。

    警告

    刪除資源群組是無法回復的動作。

  4. 在顯示的快顯視窗中輸入資源群組名稱,然後選取 [刪除]

下一步

恭喜! 您已完成這個將範本部署至 Azure 的簡介。 如果您對於意見反應區段有任何意見和建議,請讓我們知道。

下一個教學課程系列將詳細說明如何部署範本。