When you go to the Azure portal and deploy a service, you may have seen a prompt where you are asked to select an existing resource group or create a new resource group. And, a few more service deployment scenarios might ask you to select between selecting an existing or creating a new storage account.
What if you want to achieve this in your own ARM templates? You can. In ARM templates, you use the condition element within resource instance definition. Bicep supports this using the if keyword. Here is the general syntax for defining conditional deployment of a resource instance.
Unlike iterative deployments syntax, you don’t have to enclose the resource instance in square brackets. Within the if condition, you can use the comparison or logical operators supported in Bicep. Here is an example of a conditional deployment.
In the above example, the parameter newOrExisting is used to collect input on whether a new storage account needs to be created or not. The value of this parameter gets checked in the if condition within the resource instance definition. Only if the value supplied is new, a new storage account resource gets provisioned. The following ARM JSON is what this example produces.
As shown in the above example, the condition must follow the iteration syntax. This is it for today. In the next part of this series, you will learn about scoped deployments. Stay tuned.