SpyglassMTG Blog

  • Blog
  • Using Bicep Conditionals to Promote Reusability

Using Bicep Conditionals to Promote Reusability

Using Bicep Conditionals to Promote Reusability

If you are familiar with Infrastructure as Code (IaC) deployment methodologies, you understand that one of the most important benefits is being able to create reusable code modules that can meet different requirements. This allows for maximum code reuse throughout many different workloads and architectures. Azure Bicep has many language features that help enable this. In this article, I will focus specifically on looping and conditional capabilities.

One of the advantages of looping within Bicep is the ability to deploy many of the same type of resources based on a collection that is provided. For example, in the code snippets below, if you have a virtual network that has many subnets (which is typically the case), you can define the subnets as an array and then loop through each of the subnets in the array. For more information on loops in Bicep, you can go here.

Sample network parameter file

blog1

Sample network bicep file

blog2

Conditionals are great since they allow for customization of a deployment where some resources may not have all properties configured or some may not be deployed at all. The downside is that not all properties are set the same way; some are basic values like strings, boolean, integer, etc. Other properties are complex types like arrays and resource ids for other resources. The syntax of the conditional is as follows: condition ? true-value : false-value

As an example, sometimes you want to add service endpoints to some subnets but not others, or different service endpoints to different subnets. By using conditionals within the loop, you are able to abstract the differences to the parameter file versus managing multiple bicep files for each scenario. The serviceEndoint portion of the bicep template show an example of using a conditional on an array property. The conditional looks to see if the parameter object contains a “serviceEndpoint” property. If it does contain a serviceEndpoint, the array value is set to the array in the configuration, otherwise it passes null so that the array is empty.

In another example, you have some subnets you want to set route tables for but there are other subnets like GatewaySubnet and AzureBastionSubnet that can’t have a route table assigned. For route tables, it is a resource associations where you need to provide a resourceId to the “id” property. This case is different because you can’t just leave the “id” property empty, you need to provide a null json value. It uses the same conditional as service endpoints, but needs to provide additional json formatting for the id of the route table as well as using a json(‘null’) value. If the condition evaluates to true, additional Bicep markup is included, otherwise json(‘null’) is returned.

Sample network parameter file

blog3

Sample network bicep file

blog4

If you want to discuss more about looping and conditional capabilities within Azure Bicep, contact us today!

Related Posts
Microsoft How to Disable Disk Encryption on Azure VMs
Microsoft Spyglass 365: Managed IT Services