Create Infrastructure as Code in Azure Bicep

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Azure Bicep is a new declarative language that is classified as a domain-specific language (DSL) for deploying Azure resources. Everything a developer can do with an Azure Resource Manager (ARM) template can also be performed with Bicep. In fact, it is closely linked to ARM templates. Bicep makes it easier to define how your Azure resources should be configured and deployed. In this Azure development tutorial, we will demonstrate how Azure resources can be deployed using Bicep.

Read: Azure Service Bus Publisher and Subscriber

What are the Benefits of Azure Bicep?

Azure Bicep was created to improve the syntax of ARM templates. Typically, the syntax of ARM templates is cumbersome compared to other solutions. Bicep syntax, meanwhile, can more easily be compared with the ARM JSON syntax. Developers can create complex template deployments by creating smaller module Bicep files. Most importantly, Bicep will automatically detect resource dependencies, taking some of the load off of programmers.

Azure Bicep Tutorial

How to Use Azure Bicep

How to Create Azure Bicep Files

Developers can use Microsoft-provided Visual Studio Code extensions for the Bicep language to enhance the functionality that Bicep brings to the table. These extensions, specifically, provide language support and resource autocompletion to assist with creating and validating Bicep files, reducing coding errors, and making the writing of code more efficient.

To add a Bicep extension, in Visual Studio Code, select Extensions, search for Bicep, and install the extension. See the image below for more:

Azure Bicep Tutorial

Take a look at the following template Bicep code. Notice the compact code structure; it is maybe half the size of the typical ARM template. Bicep is smart enough to figure out if resources are dependent on each other. Additionally, Bicep knows it first needs to deploy appServicePlan and automatically adds the dependsOn part when it gets converted from Bicep to an ARM template. Here is the code:

param name string = 'sample-bicep-webapplication'
param location string = resourceGroup().location
param sample string = 'Sample'
param sampleCode string = 'G1'
resource webApp 'Microsoft.Web/sites@2022-01-01' = {
  name: name
  location: location
  properties: {
    name: name
    siteConfig: {
      metadata: [
        {
          name: 'TECHNOLOGY_STACK'
          value: 'dotnetcore'
        }
      ]
    }
    serverFarmId: appServicePlan.id
  }
}
resource appServicePlan 'Microsoft.Web/serverfarms@2022-01-01'  = {
  name: name
  location: location
  properties: {
    name: name
  }
  sample: {
    Tier: sample
    Name: sampleCode
  }
}

The following code snippet is used by Bicep for deployment of your resources array, with the link to the template and a link to the parameters file if available.

"resources": [
  {
    "type": "Microsoft.Resources/deployments",
    "apiVersion": "2022-01-01",
    "name": "linkedTemplate",
    "properties": {
      "mode": "Incremental",
      "templateLink": {
        "uri": "https://mystorageaccount.blob.core.windows.net/AzureTemplates/newStorageAccount.json",
        "contentVersion": "1.0.0.0"
      },
      "parametersLink": {
        "uri": "https://mystorageaccount.blob.core.windows.net/AzureTemplates/newStorageAccount.parameters.json",
        "contentVersion": "1.0.0.0"
      }
    }
  }
]

Bicep v0.3 has been integrated with the Azure Command-Line-Interface (CLI). Besides building a Bicep file, there is also a command needed to deploy your file to a resource group or subscription. The commands to do this are in the AZ Deployment group.

Read: Setting Up Budgets and Configuring Alerts in Microsoft Azure

How to Convert ARM Templates to Azure Bicep

Azure Bicep can be easily used to convert an ARM template to Bicep code. The command for this is az bicep decompile. It takes a JSON file as input and attempts to make it into Bicep.

To decompile ARM template JSON files to Bicep, use Azure CLI:

az bicep decompile --file AzureARM.json

Developers can export the template for a resource group and then pass it directly to the decompile command. Refer to the following example:

az group export --name "my_resource_group_name" > AzureARM.json
az bicep decompile --file AzureARM.json

Conclusion to Creating Infrastructure As Code Using Azure Bicep

This programming tutorial explained how to create Azure Bicep deployment templates. Be sure to check back often for more tutorials using Azure Bicep.

Read more cloud computing and Azure programming tutorials.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read