Επεξεργασία

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


Create Bicep files by using Visual Studio Code

This article shows you how to use Visual Studio Code to create Bicep files.

Install VS Code

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.

Bicep commands

Visual Studio Code comes with several Bicep commands.

Open or create a Bicep file in VS Code, select the View menu and then Command Palette. You can also use F1 or the key combination Ctrl+Shift+P to bring up the command palette. Type Bicep to list the Bicep commands.

Screenshot of Visual Studio Code Bicep commands in the command palette.

These commands include:

These commands are also shown in the context menu when you right-click a Bicep file:

Screenshot of Visual Studio Code Bicep commands in the context menu for Bicep files.

When you right-click a JSON file:

Screenshot of Visual Studio Code Bicep commands in the context menu for ARM JSON templates.

Build ARM template

The build command converts a Bicep file to an Azure Resource Manager template (ARM template). The new JSON template is stored in the same folder with the same file name. If a file with the same file name exists, it overwrites the old file. For more information, see Bicep CLI commands.

Build parameters file

The build command converts a Bicep parameters file to a JSON parameters file. The new JSON parameters file is stored in the same folder with the same file name. If a file with the same file name exists, it overwrites the old file. For more information, see Bicep CLI commands.

Create Bicep configuration file

The Bicep configuration file (bicepconfig.json) can be used to customize your Bicep development experience. You can add bicepconfig.json in multiple directories. The configuration file closest to the bicep file in the directory hierarchy is used. When you select this command, the extension opens a dialog for you to select a folder. The default folder is where you store the Bicep file. If a bicepconfig.json file already exists in the folder, you can overwrite the existing file.

To create a Bicep configuration file:

  1. Open Visual Studio Code.
  2. From the View menu, select Command Palette (or press Ctrl/Cmd+Shift+P), and then select Bicep: Create Bicep Configuration File.
  3. Select the file directory where you want to place the file.
  4. Save the configuration file when you're done.

Decompile into Bicep

This command decompiles an ARM JSON template into a Bicep file, and places it in the same directory as the ARM JSON template. The new file has the same file name with the .bicep extension. If a Bicep file with the same file name already exists in the same folder, Visual Studio Code prompts you to overwrite the existing file or create a copy.

Deploy Bicep file

You can deploy Bicep files directly from Visual Studio Code. Select Deploy Bicep file from the command palette or from the context menu. The extension prompts you to sign in Azure, select subscription, create/select resource group, and enter parameter values.

Note

The Bicep deploy command from within vscode uses the Azure Account extension for authentication. It doesn't use cloud profiles from bicepconfig.json.

Generate parameters file

This command creates a parameter file in the same folder as the Bicep file. You can choose to create a Bicep parameter file or a JSON parameter file. The new Bicep parameter file name is <bicep-file-name>.bicepparam, while the new JSON parameter file name is <bicep-file-name>.parameters.json.

Import Kubernetes manifest (Preview)

This command imports a Kubernetes manifest file, and creates a Bicep module. For more information, see Bicep extensibility Kubernetes provider, and Quickstart: Deploy Azure applications to Azure Kubernetes Service (AKS) cluster using Bicep Kubernetes provider (Preview).

Insert resource

The insert resource command adds a resource declaration in the Bicep file by providing the resource ID of an existing resource. After you select Insert Resource, enter the resource ID in the command palette. It takes a few moments to insert the resource.

You can find the resource ID by using one of these methods:


Similar to exporting templates, the process tries to create a usable resource. However, most of the inserted resources require some modification before they can be used to deploy Azure resources.

For more information, see Decompiling ARM template JSON to Bicep.

Open Bicep visualizer

The visualizer shows the resources defined in the Bicep file with the resource dependency information. The diagram is the visualization of a Linux virtual machine Bicep file.

Visual Studio Code Bicep visualizer

You can also open the visualizer side-by-side with the Bicep file.

Paste as Bicep

You can paste a JSON snippet from an ARM template to a Bicep file. Visual Studio Code automatically decompiles the JSON to Bicep. This feature is only available with the Bicep extension version 0.14.0 or newer, and it's enabled by default. To disable the feature, see VS Code and Bicep extension.

By using this feature, you can paste:

  • Full ARM JSON templates.
  • Single resource or multiple resources.
  • JSON values, such as objects, arrays, and strings. A string with double quotation marks converts to one with single quotation marks.

For example, you can start with the following Bicep file:

@description('Storage Account type')
@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_ZRS'
  'Premium_LRS'
])
param storageAccountsku string = 'Standard_LRS'

@description('Location for all resources.')
param location string = resourceGroup().location

var storageAccountName = '${uniqueString(resourceGroup().id)}storage'

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountsku
  }
  kind: 'StorageV2'
  tags: {
    ObjectName: storageAccountName
  }
  properties: {}
}

output storageAccountName string = storageAccountName

And, paste the following JSON:

{
  "type": "Microsoft.Batch/batchAccounts",
  "apiVersion": "2024-02-01",
  "name": "[parameters('batchAccountName')]",
  "location": "[parameters('location')]",
  "tags": {
    "ObjectName": "[parameters('batchAccountName')]"
  },
  "properties": {
    "autoStorage": {
      "storageAccountId": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
    }
  }
}

Visual Studio Code automatically converts the JSON to Bicep. Notice that you also need to add the parameter named batchAccountName.

You can undo the decompilation by using Ctrl+Z. The original JSON appears in the file.

Restore Bicep modules

When your Bicep file uses modules that are published to a registry, the restore command gets copies of all the required modules from the registry. It stores those copies in a local cache. For more information, see restore.

Show deployment pane

The Bicep Deployment Pane is an experimental feature. For more information, see Using the Deployment Pane.

You can also open the deployment pane side-by-side with the Bicep file.

View documentation

From Visual Studio Code, you can open the template reference for the resource type you're working on. To do so, hover your cursor over the resource symbolic name, and then select View Documentation.

Screenshot of Visual Studio Code Bicep view type document.

Go to definition

When defining a module and regardless of the type of file that's being referenced - whether it's a local file, module registry file, or a template spec - you can open the file by selecting or highlighting the module path and then press [F12]. If the referenced file is an Azure Verified Modules(AVM), you can toggle between compiled JSON or Bicep file. To open the Bicep file of a private registry module, ensure that the module is published to the registry with the WithSource switch enabled. For more information, see Publish files to registry. The Visual Studio Code Bicep extension version 0.27.1 or newer is required for opening Bicep files from a private module registry.

Troubleshoot

The Problems pane summarizes the errors and warning in your Bicep file:

Screenshot of Visual Studio Code Bicep problems pane.

For the list of error/warning codes, see Bicep error/warning codes.

Next steps

To walk through a quickstart tutorial, see Quickstart: Create Bicep files with Visual Studio Code.