Using conditional deployments with Azure Bicep

Did you know that you can set up conditional deployments with Azure Bicep?
Maybe the condition is that if this value
is true
= deploy the resource. It could also be if the var
equals prod
then deploy the resource into PROD or else deploy it into QA and use the output accordingly We can use the ?
operator in Bicep Outputs to specify our condition.
Some examples
I have prepared the following code inside my project:

Here we assume that we have two environments which are prod
and test
and we have authored one main.bicep
file which is the entrypoint for both environments. We want to achieve the following:
- If
deploy_vm
=true
- Deploy a VM & Automation Account
- If
deploy_vm
=false
- Deploy only the Automation Account
To run the deployment using the test environment I will first create a resource-group for test and submit my bicep template for deployment:
az group create \
--name rg-test-sc-demo \
--location swedencentral

Submit my template for deployment:
az deployment group create \
--template-file main.bicep \
--parameters test.bicepparam \
--resource-group rg-test-sc-demo
We can see that our conditional was successful as only an AA was deployed in Azure:

I will now do the same for PROD
az group create \
--name rg-prod-sc-demo \
--location swedencentral
az deployment group create \
--template-file main.bicep \
--parameters prod.bicepparam \
--resource-group rg-prod-sc-demo

As you can see we now have the AA and the VM (With its networking resources).
References

About me
