Upgrade to Azure Bicep

Powerful methods for migrating from ARM Templates to Azure Bicep


As cloud environments become more complex, managing infrastructure as code (IaC) efficiently is crucial. Traditionally, ARM templates have been the go-to solution for defining and deploying Azure resources. However, they can be verbose and challenging to maintain. Azure Bicep, a domain-specific language (DSL) for Azure resource management, offers a simpler, more readable, and modular approach.

This blog explores both methods and provides a detailed, step-by-step guide for generating Bicep templates using an Azure Resource ID. Whether new to Bicep or looking for a seamless migration strategy, this guide will help you streamline your Azure deployments and enhance maintainability.

What are ARM templates and Azure Bicep?

Azure Resource Manager (ARM) Templates are JSON-based files that define and deploy Azure infrastructure as code (IaC). They provide a declarative way to specify resources, configurations, and dependencies in Azure, allowing for repeatable and automated deployments. ARM templates support parameters, variables, and functions to enable flexibility and reusability, but due to their JSON syntax, they can be complex and verbose.

Azure Bicep is a domain-specific language (DSL) designed to simplify defining and deploying Azure infrastructure. It is an abstraction over ARM templates, offering a more readable and modular syntax while compiling down to standard ARM JSON templates. Bicep improves maintainability, reduces complexity, and enhances code reusability, making it a preferred choice for managing Azure resources efficiently.

2 Methods to migrate

Migrating ARM templates to Azure Bicep can be achieved through two efficient methods, mainly when dealing with existing resources in the Azure portal and transitioning your deployment templates to Azure Bicep templates:

  • Method 1: Using the ‘Decompile’ Command – A traditional approach for converting existing ARM templates.
  • Method 2: Using an Azure Resource ID – The quickest method for creating Bicep templates directly from existing Azure resources.

Templates provide a way to declaratively describe the infrastructure and configuration of resources within an Azure environment. By using Azure automation templates, you can specify the resources, dependencies, and configurations required to deploy and manage Azure services consistently and automate workloads using arm templates / Bicep templates. This allows for automated, repeatable, and version-controlled deployments across Azure environments.

In this blog, I provide an overview of Method 1 with high-level details. Method 2, on the other hand, is presented with a detailed tutorial with step-by-step instructions for implementation.

Prerequisites

To get started with preparing Bicep, you will need the following tooling:

Method 1: Generating Template – Systematic Approach of 5-Phase Workflow with Decompile Command

 

Microsoft suggests a migration in 5 phases for the Azure bicep template to convert the ARM Template {.json file} into Bicep Template {.Bicep file}.

Convert –>  Migrate –> Refactor –>Test –> Deploy

For the traditional approach of migrating ARM templates to Bicep, each phase is mentioned below:

  1. Review Existing ARM Templates: Examine your existing ARM templates to understand their structure, dependencies, and configurations.
  2. Use the ‘Decompile’ Command: Convert the ARM template to Bicep by running the az bicep decompile command. This command translates your ARM template into a corresponding Bicep file.

To Generate the Bicep File From ARM Template:

az bicep decompile –file {json_ARMtemplate_file_Path}

To Generate the parameter File: (Refer to the converted BICEP file here.)

az bicep format –file {Generated_bicep_file_path}
or
az bicep generate-params –file {Generated_bicep_file_path}

3) Refactor the Bicep Template: Once you have the Bicep file, review and modify it to optimize for readability and functionality. Adjust any hard-coded endpoint values into parameters & identify any redundant definitions, and redefine.

Redefine the Bicep code, as the generated Bicep template may not be perfect for deployment in production. This phase involves reviewing and modifying every line carefully, ensuring that connections and definitions adhere to Microsoft’s expected syntax to minimize deployment errors.

4) Test in a Staging Environment: Validate your template structure with the command below, and deploy the Bicep template in a staging environment to validate that everything works as expected. This helps catch any discrepancies between the original ARM template and the new Bicep configuration

az deployment group what-if –resource-group <resourcegroupname> –template-file <TemplateFilePath>

<resourcegroupname> — Your resource group name
<TemplateFilePath> — Your local file path where the bicep template saved{example: d:\main.Bicep}

  • After the validation is successful, deploy the bicep template using the below command:
    az deployment group create –resource-group < resourcegroupname >–template-file <TemplateFilePath>
  • If your template includes parameters, you can deploy them by specifying values: Option 1: Using a Parameter File (Recommended Approach)
  • To deploy with a parameter file, use the following command:az deployment group create –resource-group <resource-group-name> –template-file <TemplateFilePath> –parameters @<parameters-file.json>

    The @ symbol indicates that the file should be used as an input for the parameters defined in the template.Option 2: Defining Values Directly in the Deployment Command
  • Alternatively, you can define parameter values directly in the command:

    az deployment group create –resource-group <resource-group-name> –template-file <TemplateFilePath> –parameters parameterName1=value1 parameterName2=value2
    This method allows you to specify parameter values inline without using a separate parameters file5) Deploy to Production: If the testing phase is successful, you can deploy the Bicep template to your production environment using the same Azure CLI commands, ensuring a smooth transition from ARM to Bicep.This method is detailed in the Microsoft Learn page: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/migrate

Method 2: Generating Template Using an Azure Resource ID

This Azure Bicep quickstart template method uses Visual Studio Code (VS Code) to generate a Bicep template directly from an existing Azure resource. The “Resource ID” is the reference for the resource to ensure accurate Bicep conversion. Follow these steps:

Step 1: Open Visual Studio Code & Create A Bicep File

  1. Launch Visual Studio Code on your system.
  2. Make sure the Bicep extension is installed in VS Code. If not:
    1. Go to the Extensions tab.
    2. Search for “Bicep” and install the official extension.
  3. Create a new empty file with the extension of “. Bicep”

Step 2: Open the Command Palette

  1. In Visual Studio Code, press the shortcut key:
    Ctrl + Shift + P (Windows)
  2. This opens the Command Palette, a search box for executing commands.

Step 3: Insert Resource

  1. In the Command Palette, type: Insert Resource
  2. Select the “Insert Resource” option from the list.
  3. You will be prompted to enter a Resource ID.

Step 4: Get The Azure Resource ID

To retrieve the Resource ID of an existing Azure resource:

Option 1: Azure Portal

  1. Go to the Azure Portal.
  2. Navigate to the desired resource.
  3. Under the “Properties” section, find and copy the Resource ID.

Example Resource ID: /subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Logic/workflows/MyLogicApp

Option 2: Azure CLI

Run the following command to fetch the Resource ID. Here I’ve given for Logicapp resource (it varies depending on the resource type):

az resource show –resource-group <RESOURCE_GROUP> –name <LOGIC_APP_NAME> –resource-type Microsoft.Logic/workflows –query id –output tsv

Replace:

  • <RESOURCE_GROUP>: Name of the resource group where the Logic App exists.
  • <LOGIC_APP_NAME>: Name of the Logic App.
  • Microsoft.Logic/workflows: The resource type for Logic Apps in Azure.

Example: az resource show –resource-group HandsOnRG –name myStorageAccount –resource-type Microsoft.Storage/storageAccounts –query id –output tsv

Step 5: Generate Bicep Template

  1. Paste the retrieved Resource ID into the Command Palette prompt.
  2. VS Code will generate the corresponding Bicep code for the resource.
  3. The generated Bicep file will include all the properties and configurations of the resource that will be written into .Bicep file created in ‘Step1’

Step 6: Review & Refactor

The generated Bicep template may include unnecessary or hardcoded values. Refactor it as needed:

  • Replace hardcoded values with parameters or variables for flexibility.
  • Test the template to ensure it meets your deployment requirements.

The template generated would be like this:

The refactoring I’ve done for this scenario requires defining the resource group location as a parameter. If the solution needs to be deployed across different environments, a parameter file can specify values that may vary across environments, such as endpoint values. Alternatively, parameter values can also be provided directly during deployment using CLI command also.

Replaced: location: ‘southindia’ TO location: paramloc

And defined the location in parameter: param paramloc string = resourceGroup().location

Step 7: Deployment via Azure CLI

  1. Validate before deployment using the below command:az deployment group what-if –resource-group <resourcegroupname> –template-file <TemplateFilePath>
    <resourcegroupname> — Your resource group name<TemplateFilePath> — Your local file path where the bicep template saved{example: d:\main.Bicep}
  2. Deploy the bicep template using the below command:az deployment group create –resource-group < resourcegroupname >–template-file <TemplateFilePath>

    • Once deployed, verify the corresponding resource in the Azure portal.
    • As a best practice for deploying bicep templates – Always validate your Bicep templates using the what-if command before deploying.
    • Here, I used the CLI deployment method. If you opt to deploy bicep in Azure DevOps, the same scripts in the pipeline can be used with the “Deploy Using Azure CLI” task.

Summary – Migrating to Azure Bicep

We have described two powerful methods for migrating from ARM templates to Azure Bicep. Both will work, but opting for Method 1 can be tedious. The second method lets you quickly generate Bicep templates for existing Azure resources using their Resource IDs. This approach simplifies the template creation process and accurately represents resource configurations. Once the template is generated, validate it and then deploy it to Azure for a smoother migration compared to Method 1, as refactoring is minimal.

Azure Bicep is considered an emerging technology in the Azure world. It represents a shift towards more streamlined and readable infrastructure as code (IaC) practices, offering a simpler alternative to traditional ARM templates. Azure Bicep is gaining traction as organisations seek easier ways to define and manage their Azure resources, automate deployments, and improve overall deployment efficiency. As Azure continues to evolve, Bicep is expected to become even more prominent in managing and deploying Azure resources. While ARM templates will still be used for specific scenarios, Azure Bicep’s growing popularity indicates that it will become the preferred method for many in the Azure Cloud.

Contact us to discuss System Integration!

 

 

Was this article helpful?
YesNo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>