共用方式為


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

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

Tip

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

Prerequisites

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

Note

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

建立範本

您可以從 ARM 樣本建立範本規格。 複製下列範本,然後儲存為 C:\Templates\createStorageV1.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[uniqueString(resourceGroup().id)]"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    }
  ]
}

建立範本規格

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

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

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

    New-AzTemplateSpec `
      -Name storageSpec `
      -Version "1.0" `
      -ResourceGroupName templateSpecRG `
      -Location westus2 `
      -TemplateFile "C:\Templates\createStorageV1.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 資源的存取權授與群組 (部分機器翻譯)。

更新範本

若要變更範本規格中的範本,請修改範本。 下列範本類似於您先前的範本,但會新增記憶體帳戶名稱的前置詞。 複製下列範本,並將它儲存為 createStorageV2.json 檔案。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    }
  ]
}

更新範本規格版本

將名為 2.0 的新版本新增至現有範本規格,而不是為修改過的範本建立新的範本規格。您可以部署任一版本。

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

    New-AzTemplateSpec `
      -Name storageSpec `
      -Version "2.0" `
      -ResourceGroupName templateSpecRG `
      -Location westus2 `
      -TemplateFile "C:\Templates\createStorageV2.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. 從頂端功能表中選取 [刪除資源群組]。

後續步驟

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