快速入門:建立和部署範本規格

本快速入門說明如何將 Azure Resource Manager 範本 (ARM 範本) 封裝成範本規格。然後,您將部署該範本規格。您的範本規格包含會部署儲存體帳戶的 ARM 範本。

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱快速入門:使用 Bicep 建立及部署範本規格

必要條件

具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶

注意

若要使用範本規格搭配 Azure PowerShell,您必須安裝 5.0.0 版或更新版本。 若要與 Azure CLI 搭配使用,請使用 2.14.2 版或更新版本

建立範本

您可以從本機範本建立範本規格。 複製下列範本並將其儲存在名為 azuredeploy.json 的本機檔案。 本快速入門假設您已儲存至 c:\Templates\azuredeploy.json 的路徑,但是您可以使用任何路徑。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.13.1.58284",
      "templateHash": "13120038605368246703"
    }
  },
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GRS",
        "Standard_GZRS",
        "Standard_LRS",
        "Standard_RAGRS",
        "Standard_RAGZRS",
        "Standard_ZRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The storage account location."
      }
    },
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "The name of the storage account"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[parameters('storageAccountName')]"
    },
    "storageAccountId": {
      "type": "string",
      "value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
    }
  }
}

建立範本規格

範本規格是名為 Microsoft.Resources/templateSpecs 的資源類型。 若要建立範本規格,請使用 PowerShell、Azure CLI、入口網站或 ARM 範本。

  1. 建立包含範本規格的新資源群組。

    New-AzResourceGroup `
      -Name templateSpecRG `
      -Location westus2
    
  2. 在該資源群組中建立範本規格。 為新的範本規格指定 storageSpec 的名稱。

    New-AzTemplateSpec `
      -Name storageSpec `
      -Version "1.0" `
      -ResourceGroupName templateSpecRG `
      -Location westus2 `
      -TemplateFile "c:\Templates\azuredeploy.json"
    

部署範本規格

若要部署範本規格,請使用部署範本時要使用的相同部署命令。 傳入要部署的範本規格資源識別碼。

  1. 建立資源群組以包含新的儲存體帳戶。

    New-AzResourceGroup `
      -Name storageRG `
      -Location westus2
    
  2. 取得範本規格的資源識別碼。

    $id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "1.0").Versions.Id
    
  3. 部署範本規格。

    New-AzResourceGroupDeployment `
      -TemplateSpecId $id `
      -ResourceGroupName storageRG
    
  4. 您可以提供與 ARM 範本所用參數完全相同的參數。 使用儲存體帳戶類型的參數重新部署範本規格。

    New-AzResourceGroupDeployment `
      -TemplateSpecId $id `
      -ResourceGroupName storageRG `
      -storageAccountType Standard_GRS
    

授予存取權

如果您想要讓貴組織中的其他使用者部署您的範本規格,您必須授與他們讀取權限。 您可以將「讀者」角色指派給資源群組的 Microsoft Entra 群組,其中包含您想要共用的範本規格。 如需詳細資訊,請參閱教學課程:使用 Azure PowerShell 將 Azure 資源的存取權授與群組 (部分機器翻譯)。

更新範本

假設您在範本規格中發現想要執行的範本變更。下列範本類似於您先前的範本,不同之處在於其針對儲存體帳戶名稱新增了前置詞。 複製下列範本,並更新您的 azuredeploy.json 檔案。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "namePrefix": {
      "type": "string",
      "maxLength": 11,
      "defaultValue": "store",
      "metadata": {
        "description": "Prefix for storage account name"
      }
    }
  },
  "variables": {
    "storageAccountName": "[concat(parameters('namePrefix'), uniquestring(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-04-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    }
  }
}

更新範本規格版本

將名為 2.0 的新版本新增至現有範本規格,而不是針對修改過的範本建立新範本規格。使用者可以選擇任一版本來進行部署。

  1. 為範本規格建立新版本。

    New-AzTemplateSpec `
      -Name storageSpec `
      -Version "2.0" `
      -ResourceGroupName templateSpecRG `
      -Location westus2 `
      -TemplateFile "c:\Templates\azuredeploy.json"
    
  2. 若要部署新版本,請取得 2.0 版本的資源識別碼。

    $id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "2.0").Versions.Id
    
  3. 部署該版本。 提供儲存體帳戶名稱的前置詞。

    New-AzResourceGroupDeployment `
      -TemplateSpecId $id `
      -ResourceGroupName storageRG `
      -namePrefix "demoaccount"
    

清除資源

若要清除您在本快速入門中部署的資源,請刪除您所建立的兩個資源群組。

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

  2. 在 [依名稱篩選] 欄位中輸入資源群組名稱 (templateSpecRG 和 storageRG)。

  3. 選取資源群組名稱。

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

下一步

若要了解如何建立包含連結範本的範本規格,請參閱建立連結範本的範本規格