Επεξεργασία

Κοινή χρήση μέσω


Quickstart: Create Bicep files with Visual Studio

This quickstart guides you through the steps to create a Bicep file with Visual Studio. You'll create a storage account and a virtual network. You'll also learn how the Bicep extension simplifies development by providing type safety, syntax validation, and autocompletion.

Similar authoring experience is also supported in Visual Studio Code. See Quickstart: Create Bicep files with Visual Studio Code.

Prerequisites

Add resource snippet

Launch Visual Studio and create a new file named main.bicep.

Visual Studio with the Bicep extension simplifies development by providing pre-defined snippets. In this quickstart, you'll add a snippet that creates a virtual network.

In main.bicep, type vnet. Select res-vnet from the list, and then press [TAB] or [ENTER].

Screenshot of adding snippet for virtual network.

Tip

If you don't see those intellisense options in Visual Studio, make sure you've installed the Bicep extension as specified in Prerequisites. If you have installed the extension, give the Bicep language service some time to start after opening your Bicep file. It usually starts quickly, but you will not have intellisense options until it starts.

Your Bicep file now contains the following code:

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-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'
        }
      }
    ]
  }
}

This snippet contains all of the values you need to define a virtual network. However, you can modify this code to meet your requirements. For example, name isn't a great name for the virtual network. Change the name property to exampleVnet.

name: 'exampleVnet'

Notice location has a red curly underline. This indicates a problem. Hover your cursor over location. The error message is - The name "location" doesn't exist in the current context. We'll create a location parameter in the next section.

Add parameters

Now, we'll add two parameters for the storage account name and the location. At the top of file, add:

param storageName

When you add a space after storageName, notice that intellisense offers the data types that are available for the parameter. Select string.

Screenshot of adding string type to parameter.

You have the following parameter:

param storageName string

This parameter works fine, but storage accounts have limits on the length of the name. The name must have at least 3 characters and no more than 24 characters. You can specify those requirements by adding decorators to the parameter.

Add a line above the parameter, and type @. You see the available decorators. Notice there are decorators for both minLength and maxLength.

Screenshot of adding decorators to parameter.

Add both decorators and specify the character limits, as shown below:

@minLength(3)
@maxLength(24)
param storageName string

You can also add a description for the parameter. Include information that helps people deploying the Bicep file understand the value to provide.

@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

The storage account name parameter is ready to use.

Add another location parameter:

param location string = resourceGroup().location

Add resource

Instead of using a snippet to define the storage account, we'll use intellisense to set the values. Intellisense makes this step much easier than having to manually type the values.

To define a resource, use the resource keyword. Below your virtual network, type resource exampleStorage:

resource exampleStorage

exampleStorage is a symbolic name for the resource you're deploying. You can use this name to reference the resource in other parts of your Bicep file.

When you add a space after the symbolic name, a list of resource types is displayed. Continue typing storage until you can select it from the available options.

Screenshot of selecting storage accounts for resource type.

After selecting Microsoft.Storage/storageAccounts, you're presented with the available API versions. Select 2021-09-01 or the latest API version. We recommend using the latest API version.

Screenshot of selecting API version for resource type.

After the single quote for the resource type, add = and a space. You're presented with options for adding properties to the resource. Select required-properties.

Screenshot of adding required properties.

This option adds all of the properties for the resource type that are required for deployment. After selecting this option, your storage account has the following properties:

resource exampleStorage 'Microsoft.Storage/storageAccounts@2023-04-01' = {
	name: 1
	location: 2
	sku: {
		name: 3
	}
	kind: 4
}

There are four placeholders in the code. Use [TAB] to go through them and enter the values. Again, intellisense helps you. Set name to storageName, which is the parameter that contains a name for the storage account. For location, set it to location. When adding SKU name and kind, intellisense presents the valid options.

When you've finished, you have:

@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@2023-11-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@2023-04-01' = {
  name: storageName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}

For more information about the Bicep syntax, see Bicep structure.

Deploy the Bicep file

Bicep file deployment can't be done from Visual Studio yet. You can deploy the Bicep file by using Azure CLI or Azure PowerShell:

az group create --name exampleRG --location eastus

az deployment group create --resource-group exampleRG --template-file main.bicep --parameters storageName=uniquename

When the deployment finishes, you should see a message indicating the deployment succeeded.

Clean up resources

When the Azure resources are no longer needed, use the Azure CLI or Azure PowerShell module to delete the quickstart resource group.

az group delete --name exampleRG

Next steps