Bicep, do you have the muscle?

In today’s fast-paced and ever-changing world, the ability for organizations to adapt and respond quickly is becoming crucial. This necessity pushes them to be agile in multiple ways: they need to be flexible, innovative and responsive.

Being Agile effects the development process of an IT solution and the infrastructure that is needed. Infrastructure needs to be reliable and quickly be available when needed. That’s why Infrastructure as Code (IaC) is often used to automate provisioning and to manage resources. With IaC resources are setup by code, instead of manually using a user interface.

In Azure there exist Azure Resource Manager (ARM) templates that we can create as IaC solution, but it’s not the only way! Writing regular ARM templates can be complex and time consuming. With Bicep, a domain-specific-language (DSL), we can create ARM templates much easier and it offers a lot of benefits.

In this blog we will talk a little bit more about Bicep and why you should use it in your future Azure projects. Is it really the muscle you need to train?

What is Bicep?

Azure Bicep is a domain-specific language for defining ARM templates. It is designed to make the creation of ARM templates easier, by reducing the amount of lines of code needed and providing a more readable syntax for defining Azure resources.

ARM templates are actually JSON files used to define and create resources in Azure. Writing in JSON format can take a lot of time and the files are hard to maintain. The process is much easier with Bicep! It has a cleaner syntax, can be used to implement complex deployment logic and makes it possible to manage dependencies between resources.

Once you have created your Bicep files, you can deploy them to Azure Cloud. This will initiate a process to create/update/delete resources based on the definitions made within your script.

Why should I use Bicep?

If you want to setup Infrastructure as Code, Bicep has a lot of benefits to offer above the standard ARM templates. Here are a few points why you should start using Bicep scripts to deploy and manage your Azure infrastructure:

  1. Simplicity and readability: Bicep provides a cleaner and more concise syntax compared to the JSON code in the standard ARM templates. It’s a lot easier to read and write, which makes it very helpful for developers to understand and work with the code.
  2. Modularity and reusability: In your Bicep scripts you can create parameters and modules. This allows you to split code in smaller and more manageable parts. Modules can be reused across other scripts, which reduces the work to rewrite similar parts in other scripts. This modularity helps a lot, especially when there is a need to manage a large and complex infrastructure.
  3. Types safety: Bicep offers compile-time validation of your code, which helps in detecting issues in an early phase. Developers can easily check the issues and make changes where needed. In this way the amount of possible errors is limited and there is a higher chance of having a successful deployment.
  4. Easier debugging and validation: By using the Bicep built-in functions for string interpolation and resource referencing, you can easily express dependencies between resources. It also provides better error messages and validation when deploying the resources to Azure, what makes debugging a lot easier.
  5. Future proof: Microsoft is pushing Bicep as the go to language to setup and manage Azure Infrastructure. Bicep can help future-proof your infrastructure code, making sure it stays compatible with upcoming Azure features and best practices.

In summary, setting up Infrastructure as Code for your Azure infrastructure, can be very helpful, especially when you need to work together with others or in a large-scale environment. Azure Bicep offers a better way to define and deploy your resources than ARM templates. Its advantages in readability, modularity, and type safety makes it a great choice for Azure developers.

How do I create a Bicep script?

  1. Get Visual Studio Code and install the Bicep extension. The extension provides language support and resource autocompletion. It helps to create and validate your Bicep scripts.

2. Create a new file by going to the “File” menu and by clicking on the “New File…” option. A pop-up will show up to name your file. Give your file a name and use “.bicep” as suffix. Save it on a desired location, and your file is created.

3. Great, now you can start writing Bicep code! Check the Microsoft documentation for the used syntax. Keep on reading for an example!

How do I deploy a Bicep script?

When your Bicep scripts are created, you can deploy them to Azure. There are multiple ways to deploy your scripts: you have automated and manual options. The fastest way to manually deploy a Bicep script is by using Azure PowerShell or Azure CLI. Bicep comes automatically with the installation of Azure CLI. If you prefer PowerShell you need to manually install Bicep after Azure PowerShell installation. Check Microsoft documentation for more info on installation.

After installing the required software, you can manually deploy your Bicep scripts within Visual Studio Code. It’s quite quick and easy to do so. Follow the steps underneath to deploy your Bicep script.

  1. Open Visual Studio Code and open your Bicep script. In the example we are deploying a script which would create a resource group. The script is being deployed on subscription level, which is required to create resource groups.

2. Right click on the code space and choose “Deploy Bicep File…”.

3. A pop-up will show up. Give your deployment a name. Press “Enter”.

4. Another pop-up will ask you to select a parameter file. In this example we’re not using one. Select “None”.

5. Check the output to verify if the deployment was successful. You can also check the deployment in Azure by clicking the link in the output. Succeeded? Your resource group is now available in Azure!

Azure Bicep Example

Now that we know how to create and deploy bicep files, we can look at a simple example to deploy an Azure Infrastructure. An example to give you an idea about how the scripts can be built. Note that Bicep scripts can be built in different ways, especially when it comes to modularity. We won’t use a parameters file in the example, just to keep things simple.

We will setup a resource group and a storage account containing a few containers. The storage account will function as a data lake, with bronze/silver/gold layers. The setup will also deploy the same configuration to other environments (dev/acc/prd).  For each environment there will be a separate resource group, with their own storage account.

The Bicep scripts will have some kind of modularity. We will have scripts on multiple levels and will be executed on different target scopes. For example, to create a resource group we need to run the script on the scope ‘subscription’.

Bicep scripts to create:

  • main.bicep: will be our main script to deploy the whole environment and will be ran on management group scope.
  • rgtest.bicep: will deploy for a specific use-case the resource group and corresponding resources. In this script we will use another module to create our storage. This script will be ran on subscription scope.
  • medaillon-storage.bicep: module we can use to create storage account with medaillon structure. This script will be ran on resource group scope.

Follow the steps below to create the Bicep scripts and deploy them to Azure Cloud:

  1. Create the 3 bicep files in Visual Studio Code. If you like them organized you can create some folders.
    a. medaillon-storage.bicep
    b. rgtest.bicep
    c. main.bicep

2. Add the following code to the “medaillon-storage.bicep” file. In this script we define some parameters to make the file dynamic and reusable. We also add the storage account, a blob service and create 3 containers.

3. Add the code underneath to the “rgtest.bicep” file. At the top, there are some parameters defined. With this script we will deploy the resource group and reference to the module we just created to deploy the storage account.

4. Add following code to “main.bicep”. At the beginning we have an array, containing the environments we would like to deploy resources to. The module “rgtest.bicep” will be executed in a for loop, resulting a deployment for each environment of the array.

5. Deploy from your “main.bicep” script and check the deployment runs. When they succeed, your resources are available in Azure!

Wrap up!

Setting up Infrastructure as Code for Azure with Bicep brings a lot of benefits, in comparison with the standard ARM templates. Writing ARM templates in JSON code can be a tedious and time consuming job. Bicep simplifies the creation and management of ARM templates by offering a better syntax. It helps to catch errors early, which improves the code quality and reduces the amount of deployment issues. The modularity and reusability of Bicep scripts enhance collaboration and maintainability in large-scale projects. In general, Bicep would help any Azure developer and would be a powerful choice to setup Infrastructure as Code for Azure.

Jonathan Beullens

Data & analytics consultant @ Lytix