Getting started with Bicep

My friend Freek Berson just wrote a book named Getting started with Bicep. So, I thought, why not try to master Bicep? Since it will, in theory, replace ARM templating (what I mean in theory is basically you really don´t need to learn ARM templates). I´m not too fond of ARM templates since they are messy, and one can easily be confused when you have a big template, not to mention if you are debugging the template, it can be a little bit buggy to debug the bugginess. Anyhow, I just bought this book from Amazon, which you can get here, and I was going to start a small project using this book as a guideline. Of course, this would also be a little bit of a review of the book since you might take your time to decide to spend few bucks for this relatively cheap book, one might say.

For those who don´t know what ARM or Bicep is, ARM is a JSON templating engine where you can define how resources are added or managed within Azure. Microsoft Bicep is, in fact, the same but is a scripting language (DLS — Domain-specific language) where you can write more human-readable code, which is then converted into an ARM template; conversion can be done in reverse, where you covert ARM template into a Bicep code. Azure reads the ARM templates and from there used to do really cool stuff.

First, I need to prepare my OS to understand how to compile bicep files, and I would need some IDE that I would use to write this stuff. But I am already ahead since I have my favorite IDE installed, which is Visual Studio Code. So for the compiler or translator, you can use this readme if you don´t have the book. Note I´m just going to use the latest version of Bicep not to install any specific version.

Install Azure Bicep using Powershell command line

Since the Azure Bicep module is installed, I can start by doing different things like creating a resource group. I know it´s not much, but it helps me to get a feel for it. When I grasp how to code, I can say “I got a feeling” like The Black Eyed Peas song.

I start by opening an empty folder in the visual studio where my Bicep project files will be located, and from there, I create a file named “resource.bicep”. This file will be used to code into. I know I´m not doing an in-depth, step-by-step tutorial since I guess that you have some minimal knowledge of vs. code and such. But, of course, you can always buy the book or find more tutorials online.

The resource.bicep file created in VS Code

First, I need to understand how to comment since I think I am pretty good at “sometimes” to comment the code. The straightforward way to comment is to use the double forward-slash or back-slash based on where you are located in the world, US or Australia. No just joking

// Subscription Selection

Since you need to figure out what type of resource you will create, you can use Get-AzResourceProvider. This command is using pipe and filter to be able to figure out the ResourceType to be used. Note you need to log in to Azure with Connect-AzAccount to be able to use this command.

(Get-AzResourceProvider -ListAvailable | ? { $_.ProviderNamespace -like "*resources*" }).ResourceTypes | ? { $_.ResourceTypeName -like "*ResourceGroup*"}
Running the Get-AzResourceProvider

Next is the bicep code, which can be found below in the code block. Again, each line is commented on what to do.

// Subscription Selection
targetScope = 'subscription'
// Define the parameters for the group name and the location
param resourceGroupName string
param resourceGroupLocation string
// Create the resource group
resource newRG 'Microsoft.Resources/resourceGroups@2021-01-01' = {
name: resourceGroupName
location: resourceGroupLocation
}

Since we have created our first Bicep code, we can test this by deploying to azure. Let's see how this goes.

To know what location to put the resource group, you can display a list of locations that are available for you based on this command:

((Get-AzResourceProvider -ListAvailable | ? { $_.ProviderNamespace -like "*resources*" }).ResourceTypes | ? { $_.ResourceTypeName -eq "ResourceGroups"}).locations

Next is to compile the code to deploy it as an ARM code, and from there, deploy to Azure. Before we do, we need to look at something called Bicep CLI, a command-line interface where you can run the compiler or the command to convert your script into an ARM code. The command to get help for the bicep is following.

az bicep --help
Getting help for bicep CLI

We have the help form, and from there, we can try it out by typing the following command in the terminal:

az bicep build --file .\resource.bicep

We have the file resource.json created for us from that particular command, and we can then deploy the ARM template using another command line. To deploy the ARM template, we need to connect to azure.

To show you that I don´t have any resource group configured and confirm that this works, here is a snapshot of my azure portal.

No resource groups were configured.

So, to begin, we do the following, as seen in the screenshot. First, we find the “deploy a custom template,” From there, we select the JSON file that the bicep builder generated. Next, we type in the parameters that we set, and from there, we can then deploy the resource. I know this is a little bit fast, but I can assure you that this works.

Deploying ARM template through the portal

After deploying the ARM template, we can now see the resource group created and in the right location.

The resource group that was created by the ARM template

Now for the review of the book based on what I learned. The book is extensive, and it really helps you to get started with building an IaaS environment. The only issue that I found is that you have to know about Azure before you begin reading this book since you need to know what resources are in Azure, such as storage accounts, resource groups, etc. Otherwise, you would not get anything that is happening in the book. So for complete beginners/N00Bs in Azure, I would recommend taking a fundamental course.

Freek Berson gives us commands both in the Az module and the Azure CLI, so these are quite different versions of similar commands that do the same stuff. So if you prefer using either, then this book will help you understand both commands.

If you want to start learning about Microsoft Bicep and write infrastructure as a code? Then this book is a perfect start for you.

Until next time, good luck and happy Biceping…..

IT tech geek, developer etc.