Introducing Bicep PowerShell Module
As you may know I’m completely sold on Bicep, I absolutely love using it! Since a while back I use Bicep to create all my templates for Azure Deployments (as long the features I need is supported in Bicep) and I’m super excited for the upcoming release of v0.3 that will give us the missing piece of the puzzle, copy loops!
Lately I’ve been spending a lot of time creating a bunch of Bicep modules that I re-use in my templates over and over again. When I’ve spent a couple of hours coding I usually end up with a folder containing multiple .bicep
files that I need to compile running bicep build
. But since bicep build
doesn’t have a --all
switch or support wildcard characters on Windows (on OSX/Linux you can run bicep build *.bicep
), I started using a simple PowerShell function to compile all .bicep
files in a folder instead of compiling them one by one. After a couple of days using the function it escalated with more features and I decided to put together a PowerShell module that wraps Bicep CLI and share it with the community. And that’s how the Bicep PowerShell Module was born.
Commands implemented
When writing this post the following commands have been implemented in the module. For a updated list of commands see the Bicep - PowerShell Module repository.
Invoke-BicepBuild
- An equivalent tobicep build
with some additional features.- Use the
-Path
switch to specify a folder with.bicep
files in order to compile them all at the same time. - If you have work in progress in the folder that you’ve specified, you can use the
-ExcludeFile
parameter to exclude a file from compilation. - Use the
-GenerateParameterFile
switch to generate ARM Template parameter files for the compiled.bicep
file(s).
- Use the
ConvertTo-Bicep
- An equivalent tobicep decompile
.- Use the
-Path
switch to specify a folder with ARM Templates in.json
format in order to decompile them all.
- Use the
Get-BicepVersion
- Compares the installed version of Bicep CLI with the latest release.Install-BicepCLI
- Install the latest Bicep CLI release available from the Azure/Bicep repo.Update-BicepCLI
- Updates Bicep CLI if there is a newer version available.
Installing the module
The Bicep module is available at PowerShell Gallery.
Invoke-BicepBuild
Lets take a look at some of the features available in Invoke-BicepBuild
!
Compile multiple files
Here´s how you can compile all .bicep
files in a directory.
If we take a look in the directory C:\Bicep\Modules
we can see that I have a couple of Bicep files in it.
To compile all the files I can simply run Invoke-BicepBuild
if the working directory is the same directory as where my bicep modules are located in. But I have all my bicep modules in a different directory than my working directory and I also want to exclude appgw.bicep
from compilation, because it’s still a work in progress and I know it will just generate a lot of build errors. I can then run Invoke-BicepBuild -Path C:\Bicep\Modules -ExcludeFile appgw.bicep
to compile the files in the directory.
If we take a look inside the directory again we’ll see that all .bicep
files have been compiled to ARM templates except appgw.bicep
.
Generate ARM Template parameter files
When you run bicep build
to compile your .bicep
files to ARM Templates you will only get a template file. But you´ll probably end up creating a parameter file before you deploy the template. I’ve created the -GenerateParameterFile
switch to save you some time here. It will generate a parameter file containing all the parameters defined in the .bicep
file and add any default values defined as value in the parameter file. Lets have a look at how it works.
If we look at the vnet.bicep
file we compiled earlier. It has the following parameters defined:
If we compile vnet.bicep
again using the -GenerateParameterFile
switch we will now get parameter file called vnet.parameters.json
as well.
And if we look inside vnet.parameters.json
it’s a valid parameter file.
NOTE: All default values have been added to the parameter file except for
resourceGroup().location
used as default value for thevnetname
parameter. Since ARM template functions can´t be used in parameter files they are replace with empty strings instead.
ConvertTo-Bicep
The only feature added to ConvertTo-Bicep
is the possibility to decompile multiple ARM Templates to .bicep
files at the same time.
Decompile multiple ARM Templates
Here´s how you can decompile all .json
files in a directory.
We have a directory with multiple ARM Templates in it.
We can now decompile them all to .bicep
files using ConvertTo-Bicep -Path C:\ARMTemplates
When we look in the folder again we can see that .bicep
files have been generated for each ARM Template in the directory.
Get-BicepVersion
Get-BicepVersion
is a simple cmdlet to that outputs the installed version of Bicep CLI and the version number of the latest release in the Azure/Bicep repo
Check versions
To check the versions just run Get-BicepVersion
Install-BicepCLI
Install-BicepCLI
installs the latest release of Bicep CLI.
Install Bicep CLI
To install Bicep CLI run Install-BicepCLI
. If Bicep CLI is already installed a message will be outputted in the terminal.
Or:
Update-BicepCLI
Update-BicepCLI
updates Bicep CLI to the latest release.
Update Bicep CLI
To update Bicep CLI run Update-BicepCLI
. If the latest version of Bicep CLI is already installed a message will be outputted in the terminal.
Bug report and feature request
If you find a bug or have an idea for a new feature create an issue in the BicepPowerShell repo. This is also the place where you can see any planned features.
Contribution
If you like the Bicep PowerShell module and want to contribute you are very much welcome to do so. Please create an issue before you start working with a brand new feature to make sure that it’s not already in the works or that the idea has been dismissed already.
Summary
That’s it! Hope you like it!