Using conditional deployments with Azure Bicep

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:

I have hidden all the networking config to fit everything in frame, for all of the code visit my repo: https://github.com/carlzxc71/azure-bicep-conditional-output

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

Conditional deployment with Bicep - Azure Resource Manager
Describes how to conditionally deploy a resource in Bicep.

About me

About me
If you have landed on my page you will have already understood my passion for tech, but obviously there is more to life than that. Here I will try and outline a few of my other hobbies. Strength training I am a person who loves to move around and challenge