共用方式為


教學課程:將範本函式新增至 Azure Resource Manager 範本

在本教學課程中,您將瞭解如何將 範本函式 新增至 Azure Resource Manager 範本 (ARM 範本)。 您可以使用函數來動態建構值。 除了這些系統提供的範本函數之外,您還可以建立 使用者定義函數。 本教學課程需要 7 分鐘 才能完成。

先決條件

建議您完成 參數的教學課程,但這不是必要條件。

您必須有 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": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[parameters('storageName')]",
      "location": "eastus",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

假設您已將 Azure 儲存體帳戶 的位置硬式編碼至 eastus,但您必須將它部署至另一個區域。 您需要新增參數來增加範本的靈活性,並允許其具有不同的位置。

使用功能

如果您完成了 參數教程,則是使用了函數。 當您新增 "[parameters('storageName')]"時,您使用了含有參數的函數。 方括弧表示方括弧內的語法是 範本運算式。 資源管理員會解析語法,而不是將它視為常值。

函數透過在部署期間動態取得值來增加範本的靈活性。 在本教學課程中,您會使用函式來取得資源群組部署位置。

下列範例醒目提示新增名為 location的參數的變更。 參數預設值會呼叫 resourceGroup 函數。 此函式會傳回物件,其中包含已部署資源群組的相關資訊。 其中一個物件屬性是位置屬性。 當您使用預設值時,儲存體帳戶和資源群組會具有相同的位置。 群組內的資源有不同的位置。

將整個檔案複製,並以其內容替換您的範本:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "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]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[parameters('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

部署範本

在先前的教學課程中,您在美國東部建立了儲存體帳戶,但您的資源群組是在美國中部建立的。 在本教學課程中,您會在與資源群組相同的區域中建立儲存體帳戶。 使用位置的預設值,因此您不需要提供該參數值。 您必須為儲存體帳戶提供新的名稱,因為您要在不同的位置建立儲存體帳戶。 例如,使用 store2 作為前置詞,而不是 store1

如果您尚未建立資源群組,請參閱 建立資源群組。 此範例假設您已將變數設定 templateFile 為範本檔案的路徑,如 第一個教學課程所示。

New-AzResourceGroupDeployment `
  -Name addlocationparameter `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storageName "{new-unique-name}"

備註

如果部署失敗,請使用 verbose 交換器來取得所建立資源的相關資訊。 使用 debug 參數開關來獲取更多調試資訊。

確認部署

您可以從 Azure 入口網站探索資源群組來驗證部署。

  1. 登入 Azure 入口網站
  2. 從左側功能表中,選取 [資源群組]。
  3. 核取 myResourceGroup 左側的方塊,然後選取 myResourceGroup
  4. 選取您建立的資源群組。 預設名稱為 myResourceGroup
  5. 請注意,您已部署的儲存體帳戶和資源群組具有相同的位置。

清理資源

如果您要繼續進行下一個教學課程,則不需要刪除資源群組。

如果您現在要停止,您可能想要刪除資源群組。

  1. 從 Azure 入口網站,從左側功能表中選取 [資源群組]。
  2. [篩選任何欄位... ] 文字欄位中輸入資源群組名稱。
  3. 核取 myResourceGroup 旁邊的方塊,然後選取 myResourceGroup 或您的資源群組名稱。
  4. 從頂端功能表選取 [刪除資源群組 ]。

後續步驟

在本教學課程中,您會使用函數來定義參數的預設值。 在本教學課程系列中,您將繼續使用函數。 在本系列結束時,您會將函數新增至每個範本區段。