-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Bicep deployments are typically done using one parameter file that maps directly to the Bicep file. Each parameter file needs to have exact parameter match for the deployment to succeed. This leads to a situation where parameter files are hard to share for other deployments. It is also beneficial to make Bicep deployments modular. Variables can be used instead of parameters. It has both advantages and some drawbacks such as secure parameters. The advantage is that you can create a variable file with sharable variables for many related deployments. Using a YAML based variable definition makes the variable file easy to maintain, as it is more human readable than the JSON format. We will be using MS provided Bicep resource modules in the example (https://github.com/Azure/ResourceModules). These contains extensive and well defined Bicep templates with related documenation. A typical landing zone scenario with virtual network provisioning is presented.
Bicep supports loading of variables loading of JSON files with variable definitions. It is possible to further point into the json properties but this is omitted here for simplicity.
loadJsonContent('variables-dev.json')
Yaml variables need to be converted to json notation before they can be used in the deployment. Yq is a standalone tool that can be used to convert environment specific variable files for the deployment. Conversion is done at pipeline level before the deployment. The example below can use template literal expression to concatenate the environment string to the yaml variable file.
yq -P variables-dev.yml -o json > variables.json
Yaml variables with more complex data structures cannot be used in the pipeline directly. Only string, boolean and numbers are supported. A separate yaml template file can be used for shared variables between pipeline scripts and bicep deployments.
loadJsonContent('template-variables.json') // Can be used by scripts and in bicep deployments
loadJsonContent('variables.json') // Only for bicep deployments
String interpolation can be used in a additional variable template file, but the values need to be give at deployment time as in the example below:
az deployment group create \
--resource-group vnet-dev-rg \
--template-file main.bicep \
--parameters vnetName=${{ variables.vnetName }} // This will override previous vnet name if given
- Bicep resource modules, https://github.com/Azure/ResourceModules
- yq yaml <-> json converter, https://github.com/mikefarah/yq