在本教學課程中,您將瞭解如何使用 參數檔案 來儲存您在部署期間傳入的值。 在先前的教學課程中,您已將內嵌參數與部署命令搭配使用。 此方法適用於測試 Azure Resource Manager 範本 (ARM 範本),但自動化部署時,傳遞環境的一組值會更容易。 參數檔案可讓您更輕鬆地封裝特定環境的參數值。 在本教學課程中,您會建立開發和生產環境的參數檔。 此指示需要 12 分鐘 才能完成。
先決條件
建議您完成有關 標籤的教學課程,但這不是必要條件。
您必須有 Visual Studio Code 和 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": "2025-06-01",
"name": "[variables('uniqueStorageName')]",
"location": "[parameters('location')]",
"tags": "[parameters('resourceTags')]",
"sku": {
"name": "[parameters('storageSKU')]"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2025-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": "2025-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 入口網站探索資源群組來驗證部署。
- 登入 Azure 入口網站。
- 從左側功能表中,選取 [資源群組]。
- 您會看到在本教學課程中部署的兩個新資源群組。
- 選取任一資源群組,然後檢視已部署的資源。 請注意,它們符合您在該環境的參數檔案中指定的值。
清理資源
從 Azure 入口網站,從左側功能表中選取 [資源群組]。
選取核取方塊旁的超連結資源群組名稱。 如果您完成本系列,則有三個資源群組要刪除 - myResourceGroup、 myResourceGroupDev 和 myResourceGroupProd。
從頂端功能表中選取 [刪除資源群組] 圖示。
謹慎
刪除資源群組是無法回復的動作。
在顯示的快顯視窗中輸入資源群組名稱,然後選取 [刪除]。
後續步驟
祝賀。 您已完成將範本部署至 Azure 的簡介。 如果您在反饋部分有任何意見和建議,請告訴我們。
下一個教學課程系列會詳細說明部署範本。