分享方式:


教學課程:將範本函式新增至 ARM 範本

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

必要條件

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

您必須安裝 Visual Studio Code 並且與 Azure 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": {
    "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": "2021-09-01",
      "name": "[parameters('storageName')]",
      "location": "eastus",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

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

使用函式

如果您已完成參數教學課程,則可使用函式。 新增 "[parameters('storageName')]" 時,則使用參數函式。 方括弧表示括弧內的語法是範本運算式。 Resource Manager 會解析語法,而不是將其視為常值。

函式可藉由在部署期間以動態方式取得值,來為範本增加彈性。 在此教學課程中,您會使用函式來取得資源群組部署位置。

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

複製整個檔案,並以其內容取代您的範本。

{
  "$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": "2021-09-01",
      "name": "[parameters('storageName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

部署範本

在先前的教學課程中,您已在美國東部建立儲存體帳戶,但您的資源群組是在美國中部建立的。 在此教學課程中,您會在與資源群組相同的區域中建立儲存體帳戶。 使用 location 的預設值,如此就不需提供該參數值。 您必須為儲存體帳戶提供新名稱,因為您是在不同的位置建立儲存體帳戶。 例如,使用 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. 從頂端功能表中選取 [刪除資源群組]

下一步

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