Search code examples
azurevisual-studio-codeazure-bicep

Error reverse engineering Azure resource to .bicep file in Visual Studio Code


I have an Azure ASP.NET Core app and various Azure components (App Service, Function App, SQL DB) that were created manually via the Azure Portal UI. I want to reverse engineer the Azure resources to .bicep files but I get an error in VSC. Ideally I'd use Visual Studio (What I've been using for development) but there doesn't seem to be an option.

I do the following in VSC:

  1. Create an empty file myresource.bicep
  2. Get the Resource Id from the Azure extension by selecting "View properties" for the entity. E.g. "Id" property for App Service.
  3. Press ctrl+shift+p / "Bicep: Insert resource" / Paste in the Resource Id.

The error is as follows: Caught exception fetching resource: The ChainedTokenCredential failed to retrieve a token from the included credentials.

- Azure CLI not installed
- Az.Account module >= 2.2.0 is not installed.

I've checked the following:

  1. Extensions installed today: Azure Account, Azure App Service, Azure CLI Tools, Azure Contain Apps, Azure Databases, Azure Developer CLI, Azure Functions, Azure Resource.
  2. Checked that I've installed all extensions mentioned with various online tutorials.
  3. I can view each Azure resource with the Azure extension.

What am I doing wrong? I haven't typed any terminal/console messages. Am I supposed to?


Solution

  • To reverse engineer Azure resources to .bicep files, no terminal/console messages are required. You can create an empty .bicep file in Visual Studio Code and then use the Azure extension to acquire the Resource Id of the entity you want to reverse engineer. Following that, you can use the "Bicep: Insert resource" command to insert the resource declaration into the .bicep file by specifying the Resource Id of an existing resource.

    And if the module issue occurs frequently, it indicates that a module was not properly installed.

    Install Az accounts module in the VScode terminal using Install-module az.accounts command and if it is already installed it shows output warning as below.

    enter image description here

    I also got the similar caught exception when I tried in my environment.

    enter image description here

    Though you added Azure CLI extensions in VScode it wont work. So I installed Azure CLI latest release in my local windows. After the installation is done, I copied that cli folder from downloads to the .VScode in file explorer.

    enter image description here

    I restarted my Visual Studio Code once it is added in the corresponding folder. To check whether it is working fine, I gave az in VScode terminal and it shows all the commands as follows.

    enter image description here

    Then authorize it with the Azure account using

    az login --scope https://management.azure.com//.default command.

    enter image description here

    Inserted an existing resource successfully by going into Bicep: Insert a resource from command palette.

    enter image description here