다음을 통해 공유

快速入门:使用 Visual Studio Code 创建 ARM 模板

在本快速入门中,你将使用 Visual Studio Code 创建 Azure 资源管理器模板(ARM 模板)。 有关更侧重于语法的教程,请参阅教程:创建和部署第一个 ARM 模板

重要

Visual Studio Code 的 Azure 资源管理器 (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": {}
}

该模板包含以下部分:parameters、、functionsvariablesresourcesoutputs。 每个部分当前为空。

添加 Azure 资源

使用以下代码段更新资源部分以包含存储帐户。

"resources": [{
  "name": "storageaccount1",
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "2025-01-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
    }
  },

现在,在存储资源中,将名称属性更新为使用该参数。

  "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-01-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 chinaeast

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

清理资源

不再需要 Azure 资源时,请使用 Azure CLI 或 Azure PowerShell 模块删除快速入门资源组。

az group delete --name arm-vscode

后续步骤