Azure Bicep: Pump up your azure deployments

post-thumb

Azure Bicep: Pump up your azure deployments

Even though Azure Bicep was announced at Ignite 2020 I was not so keen to try it out because of the preview nature of the cli and my personal preference and past experience with beta phase tools and technology. However keeping my preference aside I decided to start learning and using bicep for managing azure resource deployments. So this post is an accumulation of my learnings so far with bicep.

Overview

Bicep is an abstraction over ARM templates that have been the bread and butter for azure deployments so far. It is a DSL (Domain Specific Language) for deploying resources in azure. It is similar to terraform’s HCL (HashiCorp Configuration Language), however it currently supports only azure and does not supports state management. It is continuously updated with support for the newer ARM Api versions as they become available. You can either generate ARM templates from bicep files for deployment or you can directly deploy bicep files, as Azure Resource Manager now supports deployments via bicep files as well.

Bicep lets you define your azure resources via less lines of code as compared to ARM which can be humongous and unmanageable sometimes. Also bicep being a DSL supports various language features which are difficult to accomplish with ARM templates.

Installation: Bicep cli

Installation is pretty easy and quick if you already have azure cli installed. Otherwise install azure cli before in order to proceed with bicep installation.

There are also other installation options available for bicep cli. However this post will follow the azure cli based installation procedure and use the set of commands available via azure cli for working with bicep.

// Upgrade azure cli
az upgrade

// Install bicep command group
az bicep install

After installing bicep command group as part of azure cli, you can use the following commands:

// View all the available bicep sub-commands
az bicep --help

Group
    az bicep : Bicep CLI command group.

Commands:
    build           : Build a Bicep file.
    decompile       : Attempt to decompile an ARM template file to a Bicep file.
    format          : Format a Bicep file.
    generate-params : Generate parameters file for a Bicep file.
    install         : Install Bicep CLI.
    list-versions   : List out all available versions of Bicep CLI.
    publish         : Publish a bicep file to a remote module registry.
    restore         : Restore external modules for a bicep file.
    uninstall       : Uninstall Bicep CLI.
    upgrade         : Upgrade Bicep CLI to the latest version.
    version         : Show the installed version of Bicep CLI.
    
// Show the installed version of Bicep CLI
az bicep version

// Upgrade Bicep CLI to the latest version
az bicep upgrade

Example usage: Decompile and deploy

The easiest way to compare bicep and ARM would be via decompiling an existing ARM template to bicep and deploying it to azure.

For this example we will be using the simplest ARM template i.e. vNet with two subnets from azure quickstarts samples. To follow along download the file locally and use the following command to decompile and deploy to azure.

 # decompile arm template to bicep
 azure bicep decompile -f .\azuredeploy.json
 
 # login to azure
 az login --use-device-code
 
 # validate bicep template
 az group validate -g <rg-name> -f .\azuredeploy.bicep
 
 # deploy bicep template
 az group deploy -g <rg-name> -f .\azuredeploy.bicep 

If you open the decompiled bicep template and compare it with the ARM template, you will find that the bicep template is more readable than the ARM template even for this simple example.

Visual Studio Code: Bicep extension

One tool that I can highly recommend for bicep is the bicep extension for VS Code. It will help you in writing bicep code from scratch using intellisense and autocomplete as well as in finding/highlighting issues within your bicep files.

Learning bicep

For learning bicep I recommend the following:

  1. MS Learn: Bicep fundamentals course
  2. Azure IaC Book