Quickstart: Create Bicep files with Visual Studio Code
This quickstart guides you through the steps to create a Bicep file with Visual Studio Code. 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. See Quickstart: Create Bicep files with Visual Studio.
Prerequisites
If you don't have an Azure subscription, create a Trial before you begin.
To set up your environment for Bicep development, see Install Bicep tools. After completing those steps, you'll have Visual Studio Code and the Bicep extension. You also have either the latest Azure CLI or the latest Azure PowerShell module.
Add resource snippet
Launch Visual Studio Code and create a new file named main.bicep.
VS Code 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 Tab or Enter.
Tip
If you don't see those intellisense options in VS Code, 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. A notification in the lower right corner indicates that the service is starting. When that notification disappears, the service is running.
Your Bicep file now contains the following code:
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
name: 'name'
location: resourceGroup().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'
You could deploy this Bicep file, but we'll add a parameter and storage account before deploying.
Add parameter
Now, we'll add a parameter for the storage account name. 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.
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.
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
Your parameter is ready to use.
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.
After selecting Microsoft.Storage/storageAccounts, you're presented with the available API versions. Select 2021-02-01.
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.
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@2021-02-01' = {
name:
location:
sku: {
name:
}
kind:
}
You're almost done. Just provide values for those properties.
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 chinaeast
. When adding SKU name and kind, intellisense presents the valid options.
When you've finished, you have:
@minLength(3)
@maxLength(24)
param storageName string
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2019-11-01' = {
name: 'examplevnet'
location: resourceGroup().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@2021-02-01' = {
name: storageName
location: 'chinaeast'
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
For more information about the Bicep syntax, see Bicep structure.
Visualize resources
You can view a representation of the resources in your file.
From the upper right corner, select the visualizer button to open the Bicep Visualizer.
The visualizer shows the resources defined in the Bicep file with the resource dependency information. The two resources defined in this quickstart don't have dependency relationship, so you don't see a connector between the two resources.
Deploy the Bicep file
Right-click the Bicep file inside the VSCode, and then select Deploy Bicep file.
From the Select Resource Group listbox on the top, select Create new Resource Group.
Enter exampleRG as the resource group name, and then press [ENTER].
Select a location for the resource group, and then press [ENTER].
From Select a parameter file, select None.
Enter a unique storage account name, and then press [ENTER]. If you get an error message indicating the storage account is already taken, the storage name you provided is in use. Provide a name that is more likely to be unique.
From Create parameters file from values used in this deployment?, select No.
It takes a few moments to create the resources. For more information, see Deploy Bicep files with visual Studio Code.
You can also deploy the Bicep file by using Azure CLI or Azure PowerShell:
az group create --name exampleRG --location chinaeast
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