Have you been struggling when writing your modular Bicep infrastructure code to try to include all possible properties in a resource definition but not require your code consumers to provide values for all properties? This is particularly true for specialized properties of a resource that are not often used in your environment but could be.
Let’s look at a Bicep resource definition for a desktop Application Group.
If you look, you can see properties for ‘plan’ and for ‘sku.' These would be defined for a specialized Marketplace offer around Azure Virtual Desktop. To deploy an application group using the above format, you would need to supply values for these properties. Trying to be clever, you try updating the definition to have the properties defined like: plan:{} and sku:{}. This will, however, cause your deployment to fail as you can’t pass a null object to them. Similarly, using string functions and conditions will yield the same result.
So, how do you get around this? You can simply omit them from the resource definition and Azure will happily create the resource for you. But you are still concerned: what if I someday need to specify these? That’s where optional parameters come in. By simply adding the ‘?’ to the end of your parameter declaration, it now becomes optional. Which means you don’t need to supply it in your parameters file or command line options. It also means you can leave those properties in your resource definition and not worry if a value is supplied or not!
Examine the module defined below based on the above resource definition to see this in action:
The optional parameter decorator has been utilized for several parameters, including those for sku, identity, and plan. These parameters, if they had not been set to optional, would cause the deployment to fail.
Additional Information:
- Azure Bicep: Bicep documentation | Microsoft Learn
- Safe-dereference Operator: Bicep safe-dereference operator - Azure Resource Manager | Microsoft Learn
- Object Functions: Bicep functions - objects - Azure Resource Manager | Microsoft Learn
- Modules: Bicep modules - Azure Resource Manager | Microsoft Learn
Be sure to see our other Azure Bicep articles:
That’s all for today. In case your curious, the example above is part of Microsoft’s Azure Virtual Desktop Solution, one of the many Advanced Specializations held by Spyglass MTG. Contact us today for more information.