bicep - using conditional when creating resources
To use conditional in Bicep, you can just specify "if" as shown below.
Also we can place a param that pass from command line during deployment.
param deploy bool
@description('Location for all resources.')
param location string = resourceGroup().location
@description('')
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-06-01-preview' =
if (deploy) {
name: 'mytestservicebusjerwo'
location: location
sku: {
name: 'Basic'
capacity: 1
tier: 'Basic'
}
}
resource serviceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2022-01-01-preview'
= if (deploy) {
parent: serviceBusNamespace
name: 'myqueue'
properties: {
lockDuration: 'PT5M'
maxSizeInMegabytes: 1024
requiresDuplicateDetection: false
requiresSession: false
//defaultMessageTimeToLive: 'P10675199DT2H48M5.4775807S'
deadLetteringOnMessageExpiration: false
duplicateDetectionHistoryTimeWindow: 'PT2M'
maxDeliveryCount: 10
//autoDeleteOnIdle: 'P10675199DT2H48M5.4775807S'
enablePartitioning: false
enableExpress: false
}
}
To run from command line
az deployment group create --resource-group exampleRg --template-file servicebus.bicep --what-if --parameters deply deploy=true
It will also be interesting to convert this into an object to make it easier to specify repeated configuration
param deploy bool
param defaultConfiguration object = {
sku: 'Basic'
duration: 'PT5M'
maxSize: 1024
capacity: 1
}
@description('Location for all resources.')
param location string = resourceGroup().location
@description('')
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-06-01-preview' = if (deploy) {
name: 'mytestservicebusjerwo'
location: location
sku: {
name: defaultConfiguration.sku
capacity: defaultConfiguration.capacity
tier: defaultConfiguration.sku
}
}
resource serviceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2022-01-01-preview' = if (deploy) {
parent: serviceBusNamespace
name: 'myqueue'
properties: {
lockDuration: defaultConfiguration.duration
maxSizeInMegabytes: defaultConfiguration.maxSize
requiresDuplicateDetection: false
requiresSession: false
//defaultMessageTimeToLive: 'P10675199DT2H48M5.4775807S'
deadLetteringOnMessageExpiration: false
duplicateDetectionHistoryTimeWindow: 'PT2M'
maxDeliveryCount: 10
//autoDeleteOnIdle: 'P10675199DT2H48M5.4775807S'
enablePartitioning: false
enableExpress: false
}
}
Comments