Optional module names inside Bicep Templates

Freek Berson
4 min readJan 20, 2024

--

This article discusses a new (experimental) feature, the ability to have optional names for modules in Bicep Templates.

Why is this interesting?

Why is this interesting, and in what scenarios can you benefit from this? Let’s take a look at an example. Below is the code of a simple Bicep module that creates a new Vnet. The module allows specifying a name, location, and vnetname.

Simple example of a Bicep Module deploying a vnet

When using this module, you need to specify a couple of things. You start with the keyword module, followed by a symbolic name, followed by the filename of the Bicep module. Next, you define the scope; in this case, the symbolic name ‘rg’ is used, which points to a resource group.

Using the vnet module inside another bicep file

Besides values for the parameters, you also need to define a name. Quite often, there is some confusion on this. The name you specify is not the name of the resource (in this case, vnet) you are going to create. When using modules, each module results in a nested deployment when the Bicep template is transpiled into an ARM Template. The name you specified here will be used as the name of the nested deployment. The screenshot below shows what the transpiled template looks like.

Bicep module reference transpiled into ARM Template

As a result, the name needs to be unique, which sometimes can be challenging. When building complex module hierarchies, where you have lots of modules referencing other modules or different naming conventions in place, it can become challenging to define unique names.

When you leave out the name property, you get a clear error indicating a missing and required property.

Error when leaving out the name property

Optional module names to the rescue!

With Bicep version 0.24, a new (currently experimental) feature is introduced to overcome the abovementioned challenges. The feature is called Optional Module Names.

To use this experimental feature in Bicep, you need to enable it explicitly. This is done by modifying the bicepconfig.json. The configuration to add or modify is optionalModuleNames, which should be set to true.

You can now specify the same module reference and actually leave out the module name without any template errors.

Module reference without name property leveraging the optionalModuleNames feature

When transpiling the Bicep template and investigating the result in JSON, you will get the below result.

Uniquely generated nested deployment name in ARM Template

The name is now formatted as:

“[format(‘vnet-{0}’, uniqueString(‘vnet’, deployment().name))]”

So, the name is now generated using the symbolic name, followed by a ‘-’ sign, followed by the function uniqueString, which takes the symbolic name and the deployment name as the parameters.

Upon deploying the Bicep template, the result of the uniquely generated nested deployment name becomes visible.

Result of uniquely generated nested deployment name in Azure portal

This concludes this article on optional module names for Bicep Templates. A great feature to avoid having to worry about unique nested deployment names!

📖 Looking to get started with Bicep? I authored and published the book Getting started with Bicep: Infrastructure as code on Azure

--

--

Freek Berson
Freek Berson

Written by Freek Berson

Empowering hybrid and remote workforces with a digital workspace from any device, anywhere. EUC Enthusiast, Microsoft MVP, 🎤 public speaker, 📚 Author.

No responses yet