Convert ARM Template to Bicep using VS Code Extension
Tags: azure, devops, json, tutorial
Published: 2023-04-11 23:49:25

## Introduction

Microsoft has just announced a new enhancement to their Bicep extension for Visual Studio code and I thought I would share this with the wider community. 

Many of us have been working with ARM templates for a long time and it is evident that Microsoft is now pushing for Bicep adoption which is leaving a lot of engineers having to refactor existing ARM template deployments to Bicep. 

Microsoft has now made this a lot easier with the new feature on the Bicep Visual Studio Code extension that allows you to paste ARM templates (JSON) into the Bicep format. 

## Pre-Requisites

1. Make sure you have the Bicep [extension ](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep)installed in Visual Studio Code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayg0348jlnuf6enj7mym.png)

2. Some JSON code that you want to convert. We will be using an Azure Container Registry for this demo. 

## Converting your template.

In this example we will convert the following template for an Azure Container Registry: 

```json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "registries_example_name": {
            "defaultValue": "example",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.ContainerRegistry/registries",
            "apiVersion": "2023-01-01-preview",
            "name": "[parameters('registries_example_name')]",
            "location": "australiaeast",
            "sku": {
                "name": "Basic",
                "tier": "Basic"
            },
            "properties": {
                "adminUserEnabled": true,
                "policies": {
                    "quarantinePolicy": {
                        "status": "disabled"
                    },
                    "trustPolicy": {
                        "type": "Notary",
                        "status": "disabled"
                    },
                    "retentionPolicy": {
                        "days": 7,
                        "status": "disabled"
                    },
                    "exportPolicy": {
                        "status": "enabled"
                    },
                    "azureADAuthenticationAsArmPolicy": {
                        "status": "enabled"
                    },
                    "softDeletePolicy": {
                        "retentionDays": 7,
                        "status": "disabled"
                    }
                },
                "encryption": {
                    "status": "disabled"
                },
                "dataEndpointEnabled": false,
                "publicNetworkAccess": "Enabled",
                "networkRuleBypassOptions": "AzureServices",
                "zoneRedundancy": "Disabled",
                "anonymousPullEnabled": false
            }
        }
    ]
}
```
1: Create a new file with the Bicep extension e.g. `test.bicep`

2: Right click on the file in Visual Studio Code and select "Paste JSON as Bicep" 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i94ndzfk6lxmo491qedk.png)

3: Your template should now be converted to Bicep! 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzn7umxcub17kcxk47wq.png)

Here is the Bicep code:

```json

param registries_example_name string = 'example'

resource registries_example_name_resource 'Microsoft.ContainerRegistry/registries@2023-01-01-preview' = {
  name: registries_example_name
  location: 'australiaeast'
  sku: {
    name: 'Basic'
    tier: 'Basic'
  }
  properties: {
    adminUserEnabled: true
    policies: {
      quarantinePolicy: {
        status: 'disabled'
      }
      trustPolicy: {
        type: 'Notary'
        status: 'disabled'
      }
      retentionPolicy: {
        days: 7
        status: 'disabled'
      }
      exportPolicy: {
        status: 'enabled'
      }
      azureADAuthenticationAsArmPolicy: {
        status: 'enabled'
      }
      softDeletePolicy: {
        retentionDays: 7
        status: 'disabled'
      }
    }
    encryption: {
      status: 'disabled'
    }
    dataEndpointEnabled: false
    publicNetworkAccess: 'Enabled'
    networkRuleBypassOptions: 'AzureServices'
    zoneRedundancy: 'Disabled'
    anonymousPullEnabled: false
  }
}

```

As you would expect some manual enhancements may be required at times, but this seems to make the process much easier.

You can also paste single lines or parts of code from JSON and it will also automatically convert them which is a nice feature.