你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本快速入门指导你完成使用 Visual Studio 创建 Bicep 文件的步骤。 你将创建一个存储帐户和一个虚拟网络。 你还将了解Bicep扩展如何通过提供类型安全性、语法验证和自动完成来简化开发。
Visual Studio Code也支持类似的创作体验。 请参阅 快速入门:使用 Visual Studio Code 创建 Bicep 文件。
先决条件
- Azure订阅。 如果没有Azure订阅,在开始之前创建免费帐户。
- Visual Studio 17.3.0 预览版3或更高版本。 请参阅 Visual Studio Preview。
- Visual Studio Bicep扩展。 请参阅 Visual Studio Marketplace。
- Bicep文件部署需要最新的 Azure CLI 或最新的 Azure PowerShell 模块。
注释
若要使 Bicep 生成的所有诊断(错误、警告和信息性消息)正确显示为 MSBuild 原生诊断,请在项目文件中配置 BicepTreatInfoAsWarnings 元素。 例如:
<Project Sdk="Microsoft.Build.NoTargets">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<BicepTreatInfoAsWarnings>true</BicepTreatInfoAsWarnings>
</PropertyGroup>
</Project>
添加资源片段
启动Visual Studio并创建名为 main.bicep 的新文件。
Visual Studio 配合 Bicep 扩展通过提供预定义的代码片段简化了开发。 在本快速入门中,你将添加用于创建虚拟网络的代码片段。
在 main.bicep 中,键入 vnet。 从列表中选择 res-vnet ,然后按 [TAB] 或 [ENTER]。
小窍门
如果在 Visual Studio 中看不到这些 intellisense 选项,请确保已安装 Prerequisites 中指定的Bicep扩展。 如果已安装该扩展,在打开Bicep文件后,请稍等片刻以便Bicep语言服务启动。 它通常启动很快,但在启动之前不会提供 intellisense 选项。
Bicep文件现在包含以下代码:
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2025-01-01' = {
name: 'name'
location: location
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
subnets: [
{
name: 'Subnet-1'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
{
name: 'Subnet-2'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
]
}
}
此代码片段包含定义虚拟网络所需的所有值。 但是,可以修改此代码以满足你的要求。 例如, name 不是虚拟网络的好名称。 将 name 属性更改为 exampleVnet.
name: 'exampleVnet'
注意 位置 具有红色大括号下划线。 这表示问题。 将光标悬停在
添加参数
现在,我们为存储帐户名称和位置添加两个参数。 在文件顶部,添加:
param storageName
在 storageName 之后添加空间时,请注意 Intellisense 提供可用于参数的数据类型。 选择 字符串。
你有以下参数:
param storageName string
此参数正常工作,但存储帐户对名称长度有限制。 该名称必须至少有三个字符,且不超过 24 个字符。 可以通过向参数添加修饰器来指定这些要求。
在参数上方添加一行,然后键入 @。 你将看到可用的修饰器。 注意minLength和maxLength都有修饰器。
添加修饰器并指定字符限制,如下所示:
@minLength(3)
@maxLength(24)
param storageName string
还可以为参数添加说明。 包括有助于部署Bicep文件的人员了解要提供的值的信息。
@minLength(3)
@maxLength(24)
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
param storageName string
存储帐户名称参数可供使用。
添加另一个位置参数:
param location string = resourceGroup().location
添加资源
我们使用 Intellisense 来设置值,而不是使用代码片段来定义存储帐户。 Intellisense 使此步骤比手动键入值更容易。
若要定义资源,请使用 resource 关键字。 在虚拟网络下方键入 资源示例Storage:
resource exampleStorage
exampleStorage 是要部署的资源的符号名称。 可以使用此名称在Bicep文件的其他部分中引用资源。
在符号名称之后添加空格时,将显示资源类型的列表。 继续键入 存储 ,直到可以从可用选项中选择存储。
在选择Microsoft.Storage/storageAccounts后,你会看到可用的 API 版本。 选择 2021-09-01 或最新的 API 版本。 建议使用最新的 API 版本。
在资源类型的单引号后,添加 = 和一个空格。 你会看到用于向资源添加属性的选项。 选择 required-properties。
此选项添加部署所需的资源类型的所有属性。 选择此选项后,存储帐户具有以下属性:
resource exampleStorage 'Microsoft.Storage/storageAccounts@2025-06-01' = {
name: 1
location: 2
sku: {
name: 3
}
kind: 4
}
代码中有四个占位符。 使用 [TAB] 浏览它们并输入值。 同样,Intellisense 可帮助你。 将 name 设置为 storageName,这是包含存储帐户名称的参数。 对于 location,将其设置为 location。 添加 SKU 名称和类型时,Intellisense 会显示有效选项。
完成后,你已具备:
@minLength(3)
@maxLength(24)
@description('Provide a name for the storage account. Use only lower case letters and numbers. The name must be unique across Azure.')
param storageName string
param location string = resourceGroup().location
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2025-01-01' = {
name: storageName
location: location
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
subnets: [
{
name: 'Subnet-1'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
{
name: 'Subnet-2'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
]
}
}
resource exampleStorage 'Microsoft.Storage/storageAccounts@2025-06-01' = {
name: storageName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
有关Bicep语法的详细信息,请参阅 Bicep 结构。
部署Bicep文件
Bicep 文件的部署尚无法通过 Visual Studio 来完成。 可以使用Azure CLI或Azure PowerShell部署Bicep文件:
az group create --name exampleRG --location eastus
az deployment group create --resource-group exampleRG --template-file main.bicep --parameters storageName=uniquename
部署完成后,应会看到一条指出部署成功的消息。
清理资源
不再需要Azure资源时,请使用Azure CLI或Azure PowerShell模块删除快速入门资源组。
az group delete --name exampleRG