Conditional deployments with Bicep!

Lately, I had to rollout Azure API Management throughout different environments.  With Bicep, of course!  In the development and test environment, we use API Management without network integration.  In the acceptance and production environments, we deploy it internal to the virtual network.  In case VNET integration is used, an Application Gateway had to be provisioned.  I had to use two types of conditional deployments.  Let’s have a look!

One of the parameters indicated whether networking should be used:

@allowed([
  'None'
  'Internal'
  'External'
])
param apimVirtualNetworkType string
Conditional deployment of a resource
Based on the virtual network type, I can determine if I should provision an Application Gateway or not.  This can be easily done through the following syntax:
resource applicationGateway 'Microsoft.Network/applicationGateways@2021-02-01' = if(apimVirtualNetworkType == 'Internal') {
 name: applicationGatewayName
 ...
}
Conditional configuration of properties
Within my API Management resource, the virtualNetworkConfiguration section should not be included when API Management will not be integrated with a virtual network.  To achieve that, we can use a conditional expression.
//Create API Management Service
resource apiManagement 'Microsoft.ApiManagement/service@2020-12-01' = {
  name: apimName
  location: location
  ...
  properties: {
    ...
    virtualNetworkConfiguration: (apimVirtualNetworkType == 'None') ? null : { 
      subnetResourceId: resourceId(apimVirtualNetworkResourceGroup, 'Microsoft.Network/virtualNetworks/subnets', apimVirtualNetworkName, apimVirtualNetworkSubnetName) 
    }
    virtualNetworkType: apimVirtualNetworkType
  }
}

Great to use this C#-inspired conditional syntax within Bicep files!

Cheers
Toon

ABOUT

MEET THE YOUR AZURE COACH TEAM

Your Azure Coach is specialized in organizing Azure trainings that are infused with real-life experience. All our coaches are active consultants, who are very passionate and who love to share their Azure expertise with you.