DEV Community

Martin
Martin

Posted on

Convert ARM Template to Bicep using VS Code Extension

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 installed in Visual Studio Code.

Image description

  1. 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:

{
    "$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
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

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

3: Your template should now be converted to Bicep!

Image description

Here is the Bicep code:


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
  }
}

Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)