Getting started with Azure Bicep – Next Generation ARM Templates

When we think about building the cloud infrastructure in Microsoft Azure platform, we always think of ARM (Azure Resource Manager) templates .There are many other ways to create resources i.e. Azure Portal, Azure PowerShell, CLI or Terraform. Most of us encounter enormous difficulties in authoring ARM templates because of complex JSON format. However, JSON format is meant to be read and write by humans as it gives you very precise control over the specific data but there are lot of punctuation that doesn’t aid human readability.

Microsoft Azure recently introduced Bicep which is a new way of deploying resources, So most of us wondering why we need yet another method for resource provisioning.

Introduction to Bicep:

Azure Bicep is a Domain Specific language(DSL) which offers a declarative way of authoring Infra-as-code to provision Azure resources. Bicep code is a transpiler to build ARM templates as Bicep offers rich validation and intellisense to write cleaner code and to assist with inserting parameters in the modules to help developers build ARM templates quicker and easier.

Pros and Cons of using Bicep

Positive:

  1. It simplifies ARM templates: Azure Bicep code is written in a simpler syntax that is easier to read and write than the JSON syntax used with ARM Templates. 
  2. It’s modular: similar to Terraform or any programming language you can modularise the deployment of VMs for example. This wasn’t really possible in ARM templates. This should lead to a repository of modules being available on GitHub, similar to Terraform Registry.

Negative:

  1. It’s limited to Azure. Bicep is a Domain specific language (DSL). If you’re using multi-cloud, want to use the same language across multiple providers, or there is a possibility of expansion to multi-cloud, this probably isn’t going to fly.
  2. Its early days: at the time of writing it’s version 0.3 and it not widely used as yet, support for loops and conditions has only just been introduced. 

Benefits of Bicep over Terraform

Support for all resource types and API versions: Bicep immediately supports all preview and GA versions for Azure services. As soon as a resource provider introduces new resources types and API versions, you can use them in your Bicep file. You don’t have to wait for tools to be updated before using the new services.

Integration with Azure services: Bicep is integrated with Azure services such as Azure Policy, template specs, and Blueprints.

No State file: we can perform a what-if/plan operation without any state file management. We use Azure as the state.

Pre-flight validation: bicep/ARM does pre-flight validation on the entire template, so you can fail a lot faster than with other tools

Getting started

Lets jump to learn how to set up Bicep development and deployment environments.

Bicep extension for Visual Studio Code.

To create and validate Bicep file, Visual studio Code provides language support and resource autocompletion. We need following prerequisites to complete below exercise

1. Create a bicep file and add Azure resources:

storageAccount.bicep:

param storageAccountName string = 'storageaccount1'

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-01-01' = {  name: storageAccountName  location: resourceGroup().location  sku: {    name:'Standard_RAGRS'    tier:'Standard'  }  kind:'StorageV2'
}

output storageAccountNameOut string = storageAccount.name

Once we have the Azure resource file, we will create a main bicep file similar as azuredeploy.json file to be deployed as the master template

main.bicep:

targetScope = 'resourceGroup'

param storageAccountName string

module storageAccount './storageAccount.bicep' = {
  name: 'storageAccountDeploy'
  params:{
    storageAccountName: storageAccountName
  }
}

Once we have the Azure resource file, we will create a main bicep file similar as azuredeploy.json file to be deployed as the master template

2. Compiling Azure Bicep code

To kick off the deployment , Bicep file needs to be converted into an ARM template (you won’t like to view the converted JSON file which looks annoyingly messy), and this is what gets deployed in the end. It looks little dodgy to me as deploying the bicep file directly makes perfect sense rather than manually converting .bicep file into JSON.

.bicep files is compiled into a single ARM Template JSON file using this command:

bicep build main.bicep

To compile multiple .bicep files into JSON ARM Template with a single command, such as:

bicep build main.bicep secondtemplate.bicep

3. Deploying Azure Bicep

The ARM Template that results from Azure Bicep compilation is deployed to Microsoft Azure just the same as any other ARM Template. This can be done using either the Azure CLI or Azure PowerShell command-line tools.

Azure PowerShell:

New-AzResourceGroupDeployment -TemplateFile main.json -ResourceGroupName rg1

Azure CLI:

az deployment group create -f main.json -g rg1

Known Limitations:

The following limits currently exist:

  • Can’t set mode or batch size on copy loops.
  • Can’t combine loops and conditions.
  • Single-line object and arrays, like ['a', 'b', 'c'], aren’t supported.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s