共用方式為


快速入門:使用 Visual Studio Code 建立 ARM 範本

在本快速入門中,您會使用 Visual Studio Code 來建立 Azure Resource Manager 範本 (ARM 範本)。 如需更著重語法的教學課程,請參閱教學課程:建立及部署第一個 ARM 範本

這很重要

適用於 Visual Studio Code 的 Azure Resource Manager (ARM) 工具延伸模組 已被取代,且在 2025 年 10 月 1 日之後將不再支援。 針對 Bicep 開發,建議您使用 Visual Studio Code 的 Bicep 延伸模組。 若要深入了解,請參閱快速入門:使用 Visual Studio Code 建立 Bicep 檔案。 請注意,即使在棄用之後,GitHub Codespaces 等「暫時安裝」方法仍會繼續運作。 要手動安裝擴展程序,您可以在 此處獲取它。

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

若要完成本快速入門,您需要 Visual Studio Code。 您也需要安裝 Azure CLIAzure PowerShell 模組,並且通過驗證。

建立 ARM 範本

使用 Visual Studio Code 建立並開啟名為 azuredeploy.json 的新檔案。

將下列 JSON 程式碼片段新增至檔案,以建立 ARM 範本:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "functions": [],
  "variables": {},
  "resources": [],
  "outputs": {}
}

範本包含下列區段: parametersfunctionsvariablesresourcesoutputs。 每個區段目前都是空的。

新增 Azure 資源

使用下列程式碼片段更新資源區段,以包含儲存體帳戶。

"resources": [{
  "name": "storageaccount1",
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "2025-06-01",
  "tags": {
    "displayName": "storageaccount1"
  },
  "location": "[resourceGroup().location]",
  "kind": "StorageV2",
  "sku": {
    "name": "Premium_LRS",
    "tier": "Premium"
  }
}],

使用 [ALT] + [SHIFT] + [F] 格式化文件,以提高可讀性。

新增範本參數

更新參數區段,以包含儲存體帳戶名稱的參數。

"parameters": {
  "storageAccountName": {
    "type": "string",
    "metadata": {
      "description": "Storage account name"
    },
    "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]"
  }
},

Azure 儲存體帳戶名稱長度下限為 3 個字元,上限為 24 個字元。 將 minLengthmaxLength 一起新增至參數,並提供適當的值。

  "parameters": {
    "storageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Storage account name"
      },
      "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]",
      "minLength": 3,
      "maxLength": 24
    }
  },

現在,在儲存體資源上,將 name 屬性更新為使用該參數。

  "resources": [
    {
      "name": "[parameters('storageAccountName')]",
      "type": "Microsoft.Storage/storageAccounts",
      ...

完成後,您的範本如下所示:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Storage account name"
      },
      "defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]",
      "minLength": 3,
      "maxLength": 24
    }
  },
  "functions": [],
  "variables": {},
  "resources": [
    {
      "name": "[parameters('storageAccountName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "tags": {
        "displayName": "storageaccount1"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "Premium_LRS",
        "tier": "Premium"
      }
    }
  ],
  "outputs": {}
}

部署範本

使用 ctrl + ` 按鍵組合開啟整合式 Visual Studio Code 終端,並使用 Azure CLI 或 Azure PowerShell 模組來部署範本。

az group create --name arm-vscode --location eastus

az deployment group create --resource-group arm-vscode --template-file azuredeploy.json 

清除資源

當您不再需要 Azure 資源時,請使用 Azure CLI 或 Azure PowerShell 模組來刪除快速入門資源群組。

az group delete --name arm-vscode

後續步驟