本快速入門說明如何將 Azure Resource Manager 範本 (ARM 範本) 封裝成範本規格 (部分機器翻譯)。然後,您會部署該範本規格。您的範本規格包含會部署儲存體帳戶的 ARM 範本。
Prerequisites
具有有效訂用帳戶的 Azure 帳戶。
免費建立帳戶。
建立範本
您可以從 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 範本。
建立包含範本規格的新資源群組。
New-AzResourceGroup `
-Name templateSpecRG `
-Location westus2
在該資源群組中建立範本規格。 為新的範本規格指定 storageSpec 的名稱。
New-AzTemplateSpec `
-Name storageSpec `
-Version "1.0" `
-ResourceGroupName templateSpecRG `
-Location westus2 `
-TemplateFile "C:\Templates\createStorageV1.json"
建立包含範本規格的新資源群組。
az group create \
--name templateSpecRG \
--location westus2
在該資源群組中建立範本規格。 為新的範本規格指定 storageSpec 的名稱。
az ts create \
--name storageSpec \
--version "1.0" \
--resource-group templateSpecRG \
--location "westus2" \
--template-file "C:\Templates\createStorageV1.json"
登入 Azure 入口網站。
搜尋範本規格。 從可用的選項中選取 [範本規格]。
選取 匯入範本,然後依照指示匯入您稍早儲存的 C:\Templates\createStorageV1.json。
提供下列值:
-
名稱:輸入範本規格的名稱。例如,storageSpec。
-
訂用帳戶:選取用來建立範本規格的 Azure 訂用帳戶。
-
資源群組:選取 [新建] 並輸入新的資源群組名稱。 例如,templateSpecRG。
-
位置:選取資源群組的位置。 例如,美國西部 2。
-
版本:輸入範本規格的版本。使用 1.0。
選取 [檢閱 + 建立],然後選取 [建立]。
Note
建議您使用PowerShell或 CLI,而不是ARM樣本來建立您的樣本規格。這些工具會自動將連結的範本轉換成與主要範本相關聯的成品。 如果您使用 ARM 範本,您必須手動將連結的範本新增為成品,這可能會更加複雜。
當您使用 ARM 範本來建立範本規格時,此範本會內嵌於資源定義中。 複製下列範本,並在本機將它儲存為 createTemplateSpec.json:
Note
在內嵌的範本中,所有範本運算式都必須以第二個左括弧進行逸出。 使用 "[[,而不是 "[。 JSON 陣列仍會使用單一左括弧。 請參閱下列範例中的名稱與位置屬性:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/templateSpecs",
"apiVersion": "2022-02-01",
"name": "storageSpec",
"location": "westus2",
"properties": {
"displayName": "Storage template spec"
}
},
{
"type": "Microsoft.Resources/templateSpecs/versions",
"apiVersion": "2022-02-01",
"name": "[format('{0}/{1}', 'storageSpec', '1.0')]",
"location": "westus2",
"properties": {
"mainTemplate": {
"$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"
}
}
]
}
},
"dependsOn": [
"storageSpec"
]
}
]
}
使用 Azure CLI 或 PowerShell 來建立新的資源群組。
New-AzResourceGroup `
-Name templateSpecRG `
-Location westus2
az group create \
--name templateSpecRG \
--location westus2
使用 Azure CLI 或 PowerShell 部署您的範本。
New-AzResourceGroupDeployment `
-ResourceGroupName templateSpecRG `
-TemplateFile "C:\Templates\createTemplateSpec.json"
az deployment group create \
--resource-group templateSpecRG \
--template-file "C:\Templates\createTemplateSepc.json"
使用 Azure CLI 或 PowerShell 確認部署。
Get-AzTemplateSpec `
-ResourceGroupName templateSpecRG `
-Name storageSpec
az ts show \
--resource-group templateSpecRG \
--name storageSpec
部署範本規格
若要部署範本規格,請使用部署範本時要使用的相同部署命令。 傳入要部署的範本規格資源識別碼。
建立資源群組以包含新的儲存體帳戶。
New-AzResourceGroup `
-Name storageRG `
-Location westus2
取得範本規格的資源識別碼。
$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "1.0").Versions.Id
部署範本規格。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG
提供與 ARM 範本所用參數完全相同的參數。 使用儲存體帳戶類型的參數重新部署範本規格。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG `
-storageAccountType Standard_GRS
建立資源群組以包含新的儲存體帳戶。
az group create \
--name storageRG \
--location westus2
取得範本規格的資源識別碼。
id=$(az ts show --name storageSpec --resource-group templateSpecRG --version "1.0" --query "id")
Note
取得範本規格識別碼,並將其指派給 Windows PowerShell 中的變數時,會發生已知問題。
部署範本規格。
az deployment group create \
--resource-group storageRG \
--template-spec $id
提供與 ARM 範本所用參數完全相同的參數。 使用儲存體帳戶類型的參數重新部署範本規格。
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters storageAccountType='Standard_GRS'
選取您建立的範本規格。 如果有很多範本,請使用搜尋方塊來尋找範本規格。
請選擇 部署。
提供下列值:
-
訂用帳戶:選取用來建立資源的 Azure 訂用帳戶。
-
資源群組:選取 [新建],然後輸入 storageRG。
選取 審核 + 建立,然後選取 建立。
複製下列範本,並在本機將它儲存為 deployTemplateSpecV1.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "demo",
"properties": {
"templateLink": {
"id": "[resourceId('templateSpecRG', 'Microsoft.Resources/templateSpecs/versions', 'storageSpec', '1.0')]"
},
"parameters": {
},
"mode": "Incremental"
}
}
]
}
在範本中, templateSpecRG 是包含範本規格的資源群組名稱, storageSpec 是範本規格的名稱, 而 1.0 是範本規格的版本。
使用 Azure CLI 或 PowerShell 為儲存體帳戶建立新的資源群組。
New-AzResourceGroup `
-Name storageRG `
-Location westus2
az group create \
--name storageRG \
--location westus2
使用 Azure CLI 或 PowerShell 部署您的範本。
New-AzResourceGroupDeployment `
-ResourceGroupName storageRG `
-TemplateFile "C:\Templates\deployTemplateSpecV1.json"
az deployment group create \
--resource-group storageRG \
--template-file "C:\Templates\deployTemplateSpecV1.json"
使用 Azure CLI 或 PowerShell 確認部署。
Get-AzResource `
-ResourceGroupName storageRG
az resource list \
--resource-group storageRG
授與存取權
若要讓貴組織中的其他使用者部署您的範本規格,請向其授與讀取權限。 將讀者角色指派給 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 的新版本新增至現有範本規格,而不是為修改過的範本建立新的範本規格。您可以部署任一版本。
為範本規格建立新版本。
New-AzTemplateSpec `
-Name storageSpec `
-Version "2.0" `
-ResourceGroupName templateSpecRG `
-Location westus2 `
-TemplateFile "C:\Templates\createStorageV2.json"
若要部署新版本,請取得 2.0 版本的資源識別碼。
$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "2.0").Versions.Id
部署該版本。 提供儲存體帳戶名稱的前置詞。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG `
-namePrefix "demoaccount"
為範本規格建立新版本。
az ts create \
--name storageSpec \
--version "2.0" \
--resource-group templateSpecRG \
--location "westus2" \
--template-file "C:\Templates\createStorageV2.json"
若要部署新版本,請取得 2.0 版本的資源識別碼。
id=$(az ts show --name storageSpec --resource-group templateSpecRG --version "2.0" --query "id")
部署該版本。 提供儲存體帳戶名稱的前置詞。
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters namePrefix='demoaccount'
開啟範本規格 storageSpec,然後選取 [建立新版本]。
選取 1.0 作為基底範本,然後選取 [ 建立]。
將新版本命名為 2.0 並選擇性地新增附註。 選取 [編輯範本]。
將範本的內容取代為更新的範本。 選取 [檢閱 + 儲存]。
選取 [儲存變更]。
若要部署新版本,請選取 [版本]。
開啟新版本,然後選取 [ 部署]。
填寫欄位,如同部署舊版時所執行的動作。
選取 審核 + 建立,然後選取 建立。
同樣地,您必須對本機範本進行一些變更,使其能夠使用範本規格。 複製下列範本,並在本機將它儲存為 createTemplateSpec.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/templateSpecs",
"apiVersion": "2022-02-01",
"name": "storageSpec",
"location": "westus2",
"properties": {
"displayName": "Storage template spec"
}
},
{
"type": "Microsoft.Resources/templateSpecs/versions",
"apiVersion": "2022-02-01",
"name": "[format('{0}/{1}', 'storageSpec', '2.0')]",
"location": "westus2",
"properties": {
"mainTemplate": {
"$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"
}
}
]
}
},
"dependsOn": [
"storageSpec"
]
}
]
}
若要將新版本新增至範本規格,請使用 Azure CLI 或 PowerShell 來部署您的範本。
New-AzResourceGroupDeployment `
-ResourceGroupName templateSpecRG `
-TemplateFile "C:\Templates\createTemplateSpec.json"
az deployment group create \
--resource-group templateSpecRG \
--template-file "C:\Templates\createTemplateSpec.json"
使用 Azure CLI 或 PowerShell 確認部署。
Get-AzTemplateSpec `
-ResourceGroupName templateSpecRG `
-Name storageSpec
az ts show \
--resource-group templateSpecRG \
--name storageSpec
您應該會在版本 2.0 清單中看到新版本。
複製下列範本,並在本機將它儲存為 deployTemplateSpecV2.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "demo",
"properties": {
"templateLink": {
"id": "[resourceId('templateSpecRG', 'Microsoft.Resources/templateSpecs/versions', 'storageSpec', '2.0')]"
},
"parameters": {
},
"mode": "Incremental"
}
}
]
}
使用 Azure CLI 或 PowerShell 部署您的範本。
New-AzResourceGroupDeployment `
-ResourceGroupName storageRG `
-TemplateFile "C:\Templates\deployTemplateSpecV2.json"
az deployment group create \
--resource-group storageRG \
--template-file "C:\Templates\deployTemplateSpecV2.json"
使用 Azure CLI 或 PowerShell 確認部署。
Get-AzResource `
-ResourceGroupName storageRG
az resource list \
--resource-group storageRG
您應該會看到一個其名稱以 store 開頭的新儲存體帳戶,以及一個基於資源群組 ID 的唯一字串。
清除資源
若要清除您在本快速入門中部署的資源,請刪除您所建立的兩個資源群組。
- 在 Azure 入口網站中,選取左側功能表中的 [資源群組]。
- 在 [依名稱篩選] 欄位中輸入資源群組名稱 (templateSpecRG 和 storageRG)。
- 選取資源群組名稱。
- 從頂端功能表中選取 [刪除資源群組]。
後續步驟
若要了解如何建立包含連結範本的範本規格,請參閱如何建立連結範本的範本規格。