Software Development Insights

Strengthening Infrastructure as Code with Bicep

Once you decide to move your application to a cloud platform, you need a way to manage your infrastructure and resources. If you choose Microsoft’s Azure as your cloud computing service, you have two options. You can write your Azure Resource Manager (ARM) templates using either JSON or Bicep for resource provisioning. 

In this article, we will focus on why you should use Bicep and how you can use it to strengthen your infrastructure.

What is Bicep?

Bicep is a language designed specifically for writing Resource Manager templates. It lets you declaratively deploy Azure resources. For example, if you try to use JSON, it might complicate things as it requires complicated expressions. You can avoid that with Bicep.

For example, check the Bicep and JSON templates below.

Source: https://docs.microsoft.com/en-us/learn/modules/introduction-to-infrastructure-as-code-using-bicep/5-how-bicep-works

The first one uses Bicep, while the second uses JSON. Notice the additional syntaxes used in JSON template. To get a better understanding, we need to know how Bicep works.

How Bicep works

Bicep works in two ways.

  1. Transpilation
  2. Decompilation

Transpilation

Azure’s Resource Manager, just like other resource managers, still requires a JSON template.  However, writing one requires additional work, as shown in the image above. To make this process, easier Bicep has developed its own set of syntaxes, which can be easily translated to JSON. Bicep uses a technique called Transpilation, during which your Bicep template is converted into a JSON template. The transpilation process happens automatically when you submit the template. You can run this process manually as well.

Source: https://docs.microsoft.com/en-us/learn/modules/introduction-to-infrastructure-as-code-using-bicep/5-how-bicep-works

Bicep allows you to define expressions, parameters, variables, strings, logical operators, deployment scopes, resources, loops, and resource dependencies. It also allows you to reference resources easily and output properties from a resource in the template.

You can further compare JSON and Bicep syntaxes here.

Decompilation

If you already have some ARM templates and you are now deciding to use Bicep, you can easily convert your old ARM templates using Decompilation. For this, you need to use the Bicep CLI

Once converted, you might notice that the original ARM template which was in JSON and the newly converted template, has different syntaxes. This is caused by converting an ARM template that was initially in JSON to Bicep and again back to JSON. However, when deployed, the converted templates also produce the same results. You might also notice some best practice issues in the converted template. So it is better to do some revisions to the template to incorporate those best practices.

Read more about decompilation here.

Pros of using Bicep

There are a lot of pros to using Bicep for template writing. We have listed them below.

Open-source

Bicep is open source and free. You can find the Bicep repo here. It is also supported by Microsoft.

Simpler syntax

Bicep provides a simpler syntax for writing resource manager templates. You can declare and reference variables and parameters directly without writing any complex functions. Bicep templates are easier to read and understand as opposed to JSON templates. Additionally, you do not need prior knowledge of programming languages.

Modules

If your Bicep template seems complex, you can always break it down into separate modules and add them to your main Bicep file when you need them. Modules enable you to simplify your code and manage it easily.

Integration with Azure services

Bicep comes integrated with other Azure services such as Azure template specs, Policy, and Blueprints.

Automatic dependency management

Bicep comes with a built-in mechanism to detect dependencies among your resources. This comes in handy during template authoring.

Support for all resource types and API versions 

Bicep supports all preview and general availability (GA) versions for Azure services. Therefore, As soon as a resource provider introduces new API versions and resource types, you can use them. 

No state or state files to manage

All state is stored in Azure. You can use the what-if operation to preview changes before deploying the template. This allows you to deploy changes confidently.

IDE support

Bicep provides extensions for Visual Studio Code includes rich syntax validation and IntelliSense for all Azure resource type API definitions. This feature enhances the authoring experience.

Decompilation

You can easily convert your existing ARM templates to Bicep ng the decompilation to decompile JSON files to Bicep.

Cons of using Bicep

Like many languages, Bicep also has cons. Lucky for us, there are only a few of them.

Bicep is only for Azure

Currently, Bicep is only available for Azure, and there are no plans to extend it. However, they are planning to provide extensibility points for some APIs that are outside of Azure.

No support for apiProfile

An apiProfile is used to map a single apiProfile to a set apiVersion for each resource type. Bicep, however, does not support the concept of apiProfile. 

Bicep is newline sensitive

You cannot write code snippets such as if conditions in multiple lines.  You have to always write it in a single line which causes readability issues.

No support for single-line objects and arrays 

Bicep does not support single-line objects and arrays such as [‘a’, ‘b’, ‘c’].

No support for user-defined functions

Bicep does not support custom user-defined functions.

Conclusion

Since Bicep is native to Azure, it’s easier to use it inside Azure. Other cloud providers such as AWS do not support Bicep. In such cases, you might have to look for other options such as open-source tools like Terraform. If your company uses only Azure, then Bicep is the best solution. It will automatically support all the new features from Azure as soon as they become available. It is also fully integrated within the Azure platform. Azure provides the Azure portal to monitor your deployments. Since it’s built specifically for this task, it is easy to write ARM templates with it. 

Leave a Reply