Azure Bicep has an inline If/Else conditional expression that can be used to conditionally provision an Azure Resource or even a full Azure Bicep Module. It’s not the same syntax as traditional If/Else statements used in may programming languages like C#, Java, or Python. However, it is really easy to use and will give you the conditional ability to decide at runtime (or rather at deployment time) whether to provision a single or multiple Azure Resources.

There are 2 ways to conditionally deploy and configure Azure Resources and modules in Azure Bicep projects:

  1. If/Else Conditional Deployment: This will enable the resource or module to be conditionally deployed based on passing in a parameter value or feature flag.
  2. If/Else Expression Parameter Assignment: This will enable the resource or module to be conditionally configured based on passing in a parameter value or feature flag.

Tutorials: If you’re looking to get started or just better knowledgeable with managing Microsoft Azure resources using Azure Bicep, then I recommend you check out the following articles:


If/Else Conditional Deployment

The if deployment condition syntax enables you to add the if condition check to the resource or module definition in the Azure Bicep code. If the condition resolves to true then the resource/module is provisioned, and if the condition resolves to false then the resource/module is not provisioned.

The parameter (other value) passed to the if condition could be a setting / configuration within the Azure Bicep project, or even a feature flag that’s passed into the project at deployment time. It can be what ever value you need it to be, so long as the condition will evaluate to a bool value. This enables you to programmatically and dynamically provision Azure Resources, and is a great way to create reusable Azure Bicep code that can be used to deploy / manage multiple different environments that are similarly configured.

Conditional Azure Resource

The following is an example of using the if keyword on the definition of an Azure Resource to be conditionally provisioned based on a parameter:

param deployVNet bool

resource hub_vnet 'Microsoft.Network/virtualnetwork@2018-05-01'= if (deployVNet) {
}

Conditional Azure Bicep Module

The following is an example of using the if keyword on the definition of an Azure Bicep Module (that will provision one or more Azure Resources within it) to be conditionally provisioned based on a parameter:

param deployHubAndSpokeNetwork bool

module hub_and_spoke_net 'hubandspoknetwork.bicep' = if (deployHubAndSpokeNetwork) {
}

If/Else Expression Parameter Assignment

In addition to conditionally provisioning Azure Resources and Azure Bicep Modules, the arguments and configurations of individual resources or modules can also be conditionally configured. This is done by using an If/Else expression to configure the parameters of an Azure Resource or Azure Bicep Module.

(condition ? true_value : false_value)

The following are a couple examples of using an If/Else expression to assign a parameter of an Azure Resource or Azure Bicep Module:

param usePrimaryRegion bool

resource hub_vnet 'Microsoft.Network/virtualnetwork@2018-05-01' = {
  location = (usePrimaryRegion ? 'eastus' : 'westus')
}

If this example, the resource will always be provisioned since it doesn’t use the if condition on the resource, but the location parameter will be conditionally configured based on the feature flag parameter within the If/Else expression assignment.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP (Azure & IoT) and HashiCorp Ambassador (2021) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to Fortune 100. He is also a Microsoft Certified Azure Solutions Architect and developer, a Microsoft Certified Trainer (MCT), and Cloud Advocate. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador (2021) Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect