Site icon 4bes.nl

Work with parameters in Bicep

When you work with Infra structure as code, you want flexibility. It wouldn’t make sense to make templates for every resource(group) you need. Instead, you can make templates reusable by using parameters. But what methods can you use? There are several options which all have their own use case. In this post, I will show you different ways to work with parameters in Bicep and what choice you can make.

Example template

To illustrate this post, I have created an example template which I want to deploy with parameters. It’s the following file:

I will show how I would add the different types of parameters to this template using a few different methods.

Add inline parameters to deployment cmdlet

The simplest way to use parameters is to do this in the New-AzResourceGroupDeployment cmdlet. When you define a template you want to deploy, in PowerShell the possible parameters will be automatically available through tab complete. This method can be combined with all the other options in this post. It will always overrule the parameters defined. This can be useful if you have a few parameters that are often the same and just want to change one or two.

This looks like this:

New-AzResourceGroupDeployment -ResourceGroupName example -TemplateFile .\main.bicep -staPrefix 'ps' -isPremiumSKU $true -tagsValues @{'parameterkind' = 'powershelldirect' } -Verbose

Pro’s

Con’s

Use case

PowerShell hash tables in PowerShell

In PowerShell you can create hash tables. These can be used as a parameterobject. The New-AzResourceGroupDeployment cmldet has the parameter TemplateParameterObject for it. If you use this in a full script with PowerShell parameters, you can create a deployment script to deploy by calling it.

You would use it like this:

$Date = Get-Date -Format 'yyyy-MM-dd'
$DeploymentParameters = @{
    staPrefix    = 'psobject'
    isPremiumSKU = $true
    tagsValues   = @{
        'parameterkind'  = 'powershellobject'
        'deploymentTime' = $Date
    }
}

$Parameters = @{
    ResourceGroupName       = 'example'
    TemplateFile            = '.\main.bicep'
    TemplateParameterObject = $DeploymentParameters
}
New-AzResourceGroupDeployment @Parameters -Verbose

(I have used splatting to keep the Parameters readable)

Pro’s

Con’s

Use case

A Parameter file

Just like with ARM templates, you are able to use a parameter-file to store your parameters. At this point in time, you will use the same files as you use with ARM templates. So that would be a file called something like main.parameters.json.
The format is json. Let’s look at how that would look for the example templates.

You will deploy the template with PowerShell or the Az Cli, by using the -TemplateParameterFile parameter.

Pro’s

Con’s

Use case

LoadTextContent with a JSON config file

This option is the only one in the bunch that is specific for Bicep (although you could work with a JSON config from a PowerShell script).
When using this option, you create a JSON file with the config that fits your needs. In this case, you don’t have to stick to formatting rules, you can structure it to your own needs. This method works very well when you combine it with one of the other parameter options. Let’s take a look at a possible solution for our template.

I created a JSON file like this:

( the namesetting level isn’t really needed for this example, I added it to show you are able to create your own categories)

Now you do need to change the template to make use of this. In this case I have added all parameters through the JSON config file as an example.

Pro’s

Con’s

Use case

Conclusion

So that is how you work with parameters in Bicep. I hope this has helped as a bit of an overview. If you want to know more about each method, you can follow the links below for the Microsoft Docs. If you have any questions, leave them in the comments.

Add inline parameters to deployment cmdlet

PowerShell objects in PowerShell

A Parameter file

LoadTextContent with a JSON config file

Exit mobile version