Generate a Bicep template from within Visual Studio Code using the Azure Bicep extension.

Generate Bicep templates from existing Azure resources with VS Code

Use Visual Studio Code to conveniently generate Azure Bicep templates for your resources, allowing you to work faster and smarter with templates!

Tobias Zimmergren
Tobias Zimmergren

ARM and Bicep templates and are often used to deploy a specific configuration of resources on infrastructure.  However, you need to carefully craft the templates to fit our scenarios and situations. Creating templates can be time consuming.

In the Azure Portal, you've had the option to export a resource, or a set of resources, as an ARM template - then later, you could convert that to Bicep.

With Visual Studio Code, the Bicep extension can generate the Bicep templates from an existing Azure resource. The extension is a great capability, and it means you can quickly prototype and design our templates from within VS Code.

I'm using Visual Studio Code to generate Bicep templates for Azure infrastructure development.

Install the extensions

I recommend using the "Azure Tools" extension, as it brings a lot of other Azure-related extensions into the bundle, including the Bicep extension.

Download the Visual Studio Code extension for Azure Tools.

You can also read more about my favorite VS Code extensions for Azure.

When you install the Azure Tools extension, you get these extensions installed in the bundle:

Find the bundled extensions in the Azure Tools Visual Studio Code extension.

By installing the Azure Tools extension and the bundled extensions, you're ready to create a new Bicep template directly from within Visual Studio Code.

You can, of course, also get the Bicep extension, but then you need to find your Resource IDs yourself. With the bundled extension, you can use the built-in resource explorer to grab the Resource IDs to generate the template. More on that below.

Generate Bicep templates

You can now bring up the command window and use the Bicep tools (on Windows, Win + P).

Command: Insert Resource

This command requires a resource ID. That is the fully qualified resource ID of any Azure Resource to which you have access.

If you do not know it by heart, that's OK.

I'm listing four easy ways you can get the Resource ID for any Azure resources you have. You will have the option to select what fits your need - and the list of options below is not complete. There are many options for optimizing how you work.

Get a Resource ID

You need to enter a Resource ID of an Azure Resource to generate the Bicep files for it. I'm lining up a few easy options here.

Option: Get the Resource ID using Visual Studio Code

Coming back to what I initially posted about the "Azure Tools" extension, it also installs the "Azure Resources" extension, allowing you to browse your resources directly from within Visual Studio Code.

I'm using Visual Studio Code to view the Properties of an Azure Resource, where I can grab the Resource ID.

You will see the JSON representation of the resource, including the Resource ID. You can copy the ID from there and insert it into the Bicep command.

Copy the Azure Resource ID from the Visual Studio Code Azure extensions.

Option: Get a Resource ID with the Azure Resources web app

One option to get the IDs of your resources is to use the Azure Resource Explorer app. I wrote about this in 2016, and it still holds today.

Read more:

Option: Get the resource ID from the Azure Portal

Another option to get the Resource ID for your resources is the Azure Portal.

You can go to "Properties" and read the "Resource ID" value for any of your resources.

Get the Resource ID for an Azure Resource from the Azure Portal.

Option: Get a resource ID using the Azure CLI

There are many ways to get a resource ID from the Azure CLI. When I work with resources, I usually know what resource group I'm looking for, or that my resources belong to a specific resource group.

With this simple command you can list all resources, and their Resource IDs:

az resource list -g "resource group name" -o table --query "[].{Name:name,ResourceID: id}"

The query asks for all resources within the given resource group, outputs the results as a table, and only displays the Name and Resource ID. From there, you can copy the ID you need.

Use the Azure CLI to get a Resource ID for an Azure resource.

Troubleshooting

Using the Bicep extension to generate the templates can generate error messages, and they are not always easy to interpret.

Here are some of the issues I encountered, and how to work around them.

Error: Failed to parse supplied resourceId "<resource id>"

This is the easy one. You likely have a typo in the resource ID. Make sure you correct it, and then try again.

Error: The ChainedTokenCredential failed

This error message was a bit trickier, but it hinted about the credentials.

Caught exception fetching resource: The ChainedTokenCredential failed due to an unhandled exception: Azure CLI authentication timed out.

I was correctly signed into the Azure account extensions using the same account as I used to generate templates. It still failed.

Workaround, which worked on 3 of my machines:

  • Sign in to Azure in Visual Studio code with the correct account.
  • Open the PowerShell/command window.
  • Sign in with the same account using the Azure CLI.
  • Voila - Visual Studio Code now understands the credential context.

Perhaps this one wasn't obvious from the error message, but now it works, and I can quickly generate all the templates I need.

AzureBicepVisual Studio Code

Tobias Zimmergren Twitter

Hey, I'm Tobias! I write about my experiences in designing, architecting, securing, and operating distributed cloud services. Nice to meet you 👋

Reactions and mentions


Hi, I'm Tobias 👋

Tobias Zimmergren profile picture

Find out more about me.

Recent comments

Recommended books!

Prepare for the AZ-500 exam!
Exam Ref AZ-500 Microsoft Azure Security Technologies
Azure Security Handbook!
Azure Security Handbook: A Comprehensive Guide for Defending Your Enterprise Environment
Prepare for the SC-900 exam!
Exam Ref SC-900 Microsoft Security, Compliance, and Identity Fundamentals