Getting started with Bicep

What is Bicep

There are many ways to provision resources into Azure, including Azure Portal, Azure CLI, or Azure PowerShell which all use Azure Resources Manager REST API’s. An alternative is to use an Infrastructure as Code (IaC) approach using Azure Resource Manager templates (ARM templates). This is a good approach to take to automate deployments as the Azure infrastructure is defined as code so can be reused and can minimizes errors. ARM Templates are a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for a project.

ARM Templates are a popular approach for DevOps engineers but they are known for being complex, hard to learn and manage. That’s why Microsoft introduced a new language called Bicep that comes with the same capabilities as ARM templates but with a cleaner syntax, improved type safety, and better support for modularity and code re-use. Bicep is an Domain Specific Language (DSL) that automatically converts to an ARM template during the deployment.

Bicep vs ARM templates

If you’re used to using ARM templates it’s relatively quick and easy to start using Bicep to deploy your Azure resources. Your Bicep code will most likely be shorter and more concise. Take the below example of creating a storage account using Bicep and ARM templates.

Bicep

Example of a Bicep template.

ARM template

Example of a ARM template

As you can see it takes a lot less Bicep code to deploy a storage account than it does using ARM templates.

You can decompile ARM templates into Bicep if you already have a library of ARM templates and want to use Bicep going forward. However, Microsoft advice the Bicep file may need revisions to implement Bicep best practises. To decompile ARM templates into Bicep, use:

az bicep decompile –file main.json

Microsoft also offer the side-by-side view at the Bicep Playground which shows the equivalent Bicep and ARM template side by side. It allows you to select sample templates or to upload your own ARM templates and see the decompiled Bicep file.

Bicep Tools

To get started with Bicep you need to install some tools.

You can use Azure CLI or Azure PowerShell to install Bicep, below is the Azure CLI command. If you don’t already have Azure CLI you can download it here, or if you want to use Azure PowerShell you can download it here.

az bicep install

Run ‘az bicep version’ to see if the install was successful, this command will return the bicep version and will also prompt if there are any upgrades available.

Visual Studio Code is a great tool to compile and edit your code. It’s a source code editor which has powerful developer tooling like IntelliSense code completion and is available on macOS, Linux, and Windows. You can download Visual Studio Code here. Once installed there’s a Bicep extension you need to install from the Visual Studio marketplace here.

Working with Bicep

Visual Studio Code’s IntelliSense makes working with Bicep files very intuitive. When you start writing your Bicep code IntelliSense will make intelligent suggestions to help you.

Example of IntelliSense.

The basic format of a Bicep file looks like this:

resource <friendlyName> ‘<type@apiversion>’=

{

property1:’value’

}

Below is a basic example of a Bicep file that creates a Log Analytics workspace.

Example of a Bicep resource.

Bicep has some helpful features which you’ll need to understand to get the most out of Bicep. These include parameters, variables, modules, and scopes.

You use parameters to make the templates more generic and reusable and to bring values in from outside the Bicep file. Define parameters at the top of the Bicep file and can use it throughout your code by referencing the parameter name. Parameters are defined by the word ‘param’ followed by the name of the parameter followed by the type of parameter. For example:

param logAnalyticsWorkspaceName string

Variables are usually defined after the parameters so you can take advantage of including parameter values in your variables.  They are similar to parameters but you use the word ‘var’ to declare it, you must provide a value for it, and you don’t define a type as Bicep can work out the type based on the value you provide. An variable might look like this:

var appServicePlanName = ‘toy-product-launch-plan’

Modules allow you to breakup your complex code into specific parts, each containing a task that’s reusable for multiple deployments. Modules are independent Bicep files which would normally contain resources that are deployed together. Using modules you can more easily reuse your Bicep code and it makes it more readable and understandable. The main Bicep template then composes multiple modules together. The below is an example of the main Bicep template calling a module.

Example of a Bicep module.

You define a module by using the ‘module’ keyword, specifying a symbolic name, and referencing the module using a module path.

Scopes in Azure Resource Manager define at which level we deploy the template to. You can target the deployment at Tenant, Management Group, Subscription, or Resource Group. You can find out more about scopes here. If you don’t specify a scope it will default to the resource group scope

Bicep Deployment

You can deploy a Bicep file from your machine to any of the scopes mentioned above using Azure CLI and Azure PowerShell. To deploy a Bicep file to a resource group you would use:

 az deployment group create –resource-group <resource-group-name> –template-file <path-to-bicep>

To deploy a Bicep file to the subscription scope you would use:

az deployment sub create –location <location> –template-file <path-to-bicep>

To deploy a Bicep file to the management group or tenant scope just change the name ‘sub’ for ‘mg’ or ‘tenant’.

You can also name the deployment and include parameters by using:

az deployment group create \

–name ExampleDeployment \

–resource-group ExampleGroup \

–template-file <path-to-bicep> \

–parameters storageAccountType=Standard_GRS

Or to use a local parameter file, use @ to specify the local file name:

az deployment group create \

 –name ExampleDeployment \

 –resource-group ExampleGroup \

 –template-file storage.bicep \

 –parameters @storage.parameters.json

Hopefully this has given you a quick introduction to Bicep. If you want to learn more you can access the Bicep Quickstart and the Bicep Microsoft Learn pages which will continue you on your Bicep journey.

About the author