2

Azure Bicep: If/Else Conditional Resource And Module Deployment | Build5Nines

 1 year ago
source link: https://build5nines.com/azure-bicep-if-else-conditional-resource-and-module-deployment/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Azure Bicep: If/Else Conditional Resource and Module Deployment

By Chris Pietschmann | January 19, 2023 - 2:59 PM EST (19:59 UTC)
Category: Azure Bicep
Azure Bicep: If/Else Conditional Resource and Module Deployment

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.

About the Author

d565ce4d3fdf8007e1d707362cca9465?s=128&d=identicon&r=gMicrosoft MVP

Chris Pietschmann

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.

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK