你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

适用于 Visual Studio Code 的 Azure 资源管理器工具提供语言支持、资源片段和资源自动完成。 这些工具可帮助创建和验证 Azure 资源管理器模板(ARM 模板),因此是建议的 ARM 模板创建和配置方法。 在本快速入门中,你将使用扩展从头开始创建 ARM 模板。 在此过程中,你将体验 ARM 模板片段、验证、完成和参数文件支持等扩展功能。

本快速入门重点介绍如何使用 Visual Studio Code 扩展生成 ARM 模板。 有关更侧重于语法的教程,请参阅教程:创建和部署第一个 ARM 模板

若要完成本快速入门,需要使用装有 Azure 资源管理器工具扩展Visual Studio Code。 此外,需要安装 Azure CLIAzure PowerShell 模块并完成身份验证。

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户

提示

我们建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且该语法更易于使用。 有关详细信息,请参阅快速入门:使用 Visual Studio Code 创建 Bicep 文件

注意

适用于 Visual Studio Code 的 Azure 资源管理器工具扩展的当前版本无法识别 languageVersion 2.0 中提供的增强功能。

创建 ARM 模板

使用 Visual Studio Code 创建并打开名为 azuredeploy.json 的新文件。 在代码编辑器中输入 arm,该命令将启动用于创建 ARM 模板基架的 Azure 资源管理器片段。

选择 arm! 以创建一个仅限用于 Azure 资源组部署的模板。

Screenshot showing Azure Resource Manager scaffolding snippets.

此片段将创建 ARM 模板的构建基块。

Screenshot showing a fully scaffolded ARM template.

请注意,Visual Studio Code 语言模式从“JSON”更改为“Azure 资源管理器模板”。 该扩展包含特定于 ARM 模板的语言服务器,该服务器提供特定于模板的验证、完成和其他语言服务。

Screenshot showing Azure Resource Manager as the Visual Studio Code language mode.

添加 Azure 资源

该扩展包含许多 Azure 资源的片段。 使用这些片段可以轻松地将资源添加到模板部署中。

将光标置于模板的 resources 块中,键入 storage,然后选择“arm-storage”片段。

Screenshot showing a resource being added to the ARM template.

此操作会将一个存储资源添加到模板。

Screenshot showing an Azure Storage resource in an ARM template.

可以使用 Tab 键在存储帐户的各个可配置属性之间切换。

Screenshot showing how the tab key can be used to navigate through resource configuration.

完成和验证

该扩展的最强大功能之一是与 Azure 架构的集成。 Azure 架构为扩展提供了验证和资源感知的完成功能。 让我们修改存储帐户,通过实际操作来了解验证和完成。

首先,将存储帐户类型更新为某个无效值,例如 megaStorage。 可以看到,此操作导致生成了一条警告,指出 megaStorage 不是有效值。

Screenshot showing an invalid storage configuration.

若要使用完成功能,请删除 megaStorage,将光标置于双引号内部,然后按 ctrl + space。 此操作会显示有效值的完成列表。

Screenshot showing extension auto-completion.

添加模板参数

现在,创建并使用一个参数来指定存储帐户名称。

将光标置于 parameters 块内,添加一个回车符,键入 ",然后选择 new-parameter 片段。 此操作会将一个泛型参数添加到模板。

Screenshot showing a parameter being added to the ARM template.

将参数名称更新为 storageAccountName,将说明更新为 Storage Account Name

Screenshot showing the completed parameter in an ARM template.

Azure 存储帐户名称的最小长度为 3 个字符,最大长度为 24 个字符。 将 minLengthmaxLength 添加到参数,并提供适当的值。

Screenshot showing minLength and maxLength being added to an ARM template parameter.

现在,在存储资源中,将名称属性更新为使用该参数。 为此,请删除当前名称。 输入双引号和左方括号 [,这会生成 ARM 模板函数的列表。 从列表中选择“parameters”。

Screenshot showing auto-completion when using parameters in ARM template resources.

在圆括号内部输入单引号 ' 会生成模板中定义的所有参数(在本例中为 storageAccountName)的列表。 选择该参数。

Screenshot showing completed parameter in an ARM template resource.

创建参数文件

使用 ARM 模板参数文件可以存储特定于环境的参数值,并在部署时以组的形式传递这些值。 例如,可以创建一个参数文件来包含特定于测试环境的值,并创建另一个参数文件以用于生产环境。

在扩展中可以从现有模板轻松创建参数文件。 若要创建,请在代码编辑器中右键单击模板,然后选择 Select/Create Parameter File

Screenshot showing the right-click process for creating a parameter file from an ARM template.

选择 New>All Parameters>,然后选择参数文件的名称和位置。

此操作会创建一个新的参数文件,并将该文件映射到从其创建了该文件的模板。 选择模板后,可以在 Visual Studio Code 状态栏中查看和修改当前的模板/参数文件映射。

Screenshot showing the template/parameter file mapping in the Visual Studio Code status bar.

现在,参数文件映射到模板,扩展会将模板和参数文件一同验证。 若要通过实际操作来了解此验证,请将一个双字符值添加到参数文件中的 storageAccountName 参数,然后保存该文件。

Screenshot showing an invalidated template due to parameter file issue.

导航回 ARM 模板,此时可以看到一个错误,指出值不符合参数条件。

Screenshot showing a valid ARM template.

将值更新为某个适当值,保存文件,然后导航回到模板。 可以看到,有关参数的错误已解决。

部署模板

使用 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 --parameters azuredeploy.parameters.json

清理资源

如果不再需要本文中创建的 Azure 资源,请使用 Azure CLI 或 Azure PowerShell 模块删除快速入门资源组。

az group delete --name arm-vscode

后续步骤