Supercharging your IaC Experience with Azure Bicep


Posted on 3/4/2021


When you think about Infrastructure as Code (IaC) in Azure, you think about ARM (Azure Resource Manager) templates.

Even though IaC has many advantages, ARM are not always **easy** to author.

But ARM has become a lot cooler now as Microsoft announce Azure Bicep a while ago. As one of the announcements of the Microsoft Ignite 2021, Bicep has been released for productive use.

How to start

First go to GitHub and get the latest version of the Bicep CLI If you don't have VSCode already, I highly recommend to use it as there this amazing extension for Bicep.

Microsoft already published a lot of examples on GitHub feel free to check those out as well.

Write your first Bicep file

Lets get started with a basic storage account. Open Visual Studio Code and create a new git repository (or just a folder) on your machine, and create a file "main.bicep". Now we can start adding our resources. The basic resource syntax looks like this:

resource Identifier 'Microsoft.Provider/Type@Version' = {
  name: 
  location: 
  properties: {
    
  }
}

The "Identifier" can be compared to a variable in any other programming language. You can reference it and access the properties of the resource. Give an appropriate name for the resource here. We will use "storageAccount" in this case here.

Next the "Provider Namespace", this attribute matches with the namespaces and versions we already know from ARM. We will create a basic storage account here, so we will use "Microsoft.Storage/storageAccounts@2019-06-01". Location should be also known from ARM, we can use any valid Azure region here. E.g. westeurope

For a Storage Account we also need to specify the "kind", "name" and "sku". Those should be also quite familiar from ARM.

The full template could look like this:

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: 'uniquestorage001' 
  location: 'westeurope'
  kind: 'Storage'
  sku: {
    name: 'Standard_LRS'
  }
}

Next we could do some refactoring to use variables and parameters so our template is more agile and reusable.

param name string = 'uniquestorage001'

var location = 'westeurope'

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: name
  location: location
  kind: 'Storage'
  sku: {
    name: 'Standard_LRS'
  }
}

How to deploy bicep

Now comes the cool part. Bicep can be compiled to ARM templates using the bicep cli. This way you can use your already established tooling to deploy to Azure. (e.g. Azure DevOps, GitHub, PowerShell, etc.)

Run the following command to compile the code:

bicep build main.bicep

Alternatively you can also deploy .bicep files using the same tools.

az deployment group create -f ./main.bicep -g my-rg

I hope you'll have a lot of fun with this new approach.