if-elseif-else in Azure Bicep

Thomas Pentenrieder
medialesson
Published in
2 min readJul 31, 2023

Azure Bicep supports conditional deployments making it easy to create resources depending on external factors such as the current environment. And while the official docs show a simple example with just if-statements it’s also possible to have a bit more complex scenarios with multiple chained if-else-blocks.

Bicep does not have normal if-else-blocks as known from basically any programming language. It does support the ternary conditional operator though so you can write things like:

var res = isProd ? prodResource : testResource

And by chaining you can add multiple conditions, e.g.:

var res = isProd ? prodResource : isTest ? testResource : devResource

Below you can see how this might be used in a bicep file to create different storage accounts depending on the stage and returning the name as an output. For brevity reasons we’re working with existing resource here. Not that mult-line statements are also supported to keep the code readable.

if-elseif-else in Azure Bicep

Originally published at https://medienstudio.net on July 31, 2023.

--

--