Bicep optional parameters: ?, .? and ?? with conditional resources
Most Bicep modules grow a pile of optional settings. Callers should be able to leave them out, and the module should pick a default. Bicep has three small features for that: nullable types (?), safe dereference (.?) and coalesce (??). Combined with if on a resource, you can cover most “optional” cases without a wall of union() and contains().
The module
param namePrefix string = 'stdemo'
param location string = resourceGroup().location
@description('Optional: only create a diagnostics container when true')
param enableLogsContainer bool = false
@description('Optional settings object; the caller may leave out any property')
param options {
minTlsVersion: string?
sku: string?
tags: object?
}?
var sku = options.?sku ?? 'Standard_LRS'
var minTls = options.?minTlsVersion ?? 'TLS1_2'
resource sa 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: '${namePrefix}${uniqueString(resourceGroup().id)}'
location: location
kind: 'StorageV2'
sku: { name: sku }
tags: options.?tags ?? {}
properties: {
minimumTlsVersion: minTls
allowBlobPublicAccess: false
}
}
resource blobSvc 'Microsoft.Storage/storageAccounts/blobServices@2023-05-01' = {
parent: sa
name: 'default'
}
resource logs 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-05-01' = if (enableLogsContainer) {
parent: blobSvc
name: 'logs'
}
output logsContainerId string = enableLogsContainer ? logs.id : 'not-created'
What each piece does:
options {...}?the whole object is optional, and each property inside is optional too (string?).options.?skureads the property and givesnullif the object or the property is missing, instead of failing.?? 'Standard_LRS'supplies the default when the left side isnull.= if (enableLogsContainer)creates the resource only when the condition is true.
bicep build main.bicep exited 0 with no messages.
What it compiles to
I looked at the generated ARM JSON to confirm the features map to the functions I expected:
"sku": "[coalesce(tryGet(parameters('options'), 'sku'), 'Standard_LRS')]",
"minTls": "[coalesce(tryGet(parameters('options'), 'minTlsVersion'), 'TLS1_2')]",
"tags": "[coalesce(tryGet(parameters('options'), 'tags'), createObject())]",
and the conditional container carries:
condition: [parameters('enableLogsContainer')]
The ternary in the output compiled to if(parameters('enableLogsContainer'), resourceId(...), 'not-created'), so the output does not try to read a resource that was never created.
The mistake: skipping .? on an optional object
Here is the same idea written carelessly:
param unusedThing string = 'x'
param options {
sku: string?
}?
resource sa '...' = {
...
sku: {
name: options.sku
}
}
bicep build still exited 0, and both bicep build and bicep lint printed the same three warnings:
Warning no-unused-params: Parameter "unusedThing" is declared but never used.
Warning BCP321: Expected a value of type "string" but the provided value is of type "null | string".
Warning BCP318: The value of type "null | { sku: null | string }" may be null at the start of the deployment, which would cause this access expression (and the overall deployment with it) to fail.
BCP318 is the one to take seriously: it is the compiler telling you the deployment can fail at runtime.
Gotcha: warnings do not fail your pipeline
bicep lint exited 0 even with those three warnings. If your CI only checks the exit code, it passes. Two ways to tighten it, both tested:
- Raise a linter rule to
errorinbicepconfig.json:
{
"analyzers": {
"core": {
"rules": {
"no-unused-params": { "level": "error" }
}
}
}
}
With this config bicep lint printed Error no-unused-params ... and exited 1. The two BCP warnings stayed warnings. This file configures linter rules; I did not test whether it can change the compiler’s own BCP diagnostics, so the grep gate below is the safe option for those.
- Fail on specific compiler warnings with a grep gate in CI, for example:
bicep build main.bicep --stdout 2>&1 >/dev/null | grep -q "BCP318" && exit 1
That matched the BCP318 line in my test. Decide which warning codes are fatal for your team and list them explicitly.
Reproduce it
Both Bicep files, the config, and the script that generated every output on this page are in the evidence folder (./run.sh).