Bicep: Exploring User-Defined Data Types and Bicep Parameter Files

Ousmane Barry
2 min readJun 19, 2023

--

No need to introduce Bicep, which has become the flagship and indispensable tool for deploying resources on Azure. Azure Bicep is an infrastructure as code (IaC) description language that aims to simplify the definition and deployment of Azure resources. In this article, we will explore two key features of Bicep: user-defined data types and Bicep parameter files.

Code sample in Github: https://github.com/Thialala/bicep-modules/tree/main/modules/storage-accounts

User-Defined Data Types

As of the time of writing, the user-defined data types feature is in preview and needs to be enabled by modifying the bicepconfig.json file to include the following json:

{
"experimentalFeaturesEnabled": {
"userDefinedTypes": true
}
}

Once enabled, user-defined data types allow us to define our own data types, which can help make the code more readable and easier to maintain. For example, in the Bicep code below from file main.bicep, we define two data types: storageAccountSkuType and storageAccountConfigType:

type storageAccountSkuType = 'Standard_LRS' | 'Standard_ZRS' | 'Standard_GRS'
type storageAccountConfigType = {
name: string
sku: storageAccountSkuType
}

param storageAccountConfig storageAccountConfigType
param location string = resourceGroup().location

Here, storageAccountSkuType is an string type with three allowed values that represent the different types of SKUs a storage account can have, while storageAccountConfigType is an object type that contains two properties: name and sku which is of type storageAccountSkuType (nested type usage 😃). These user-defined data types can then be used as types for parameters in our Bicep template, as shown in the line param storageAccountConfig storageAccountConfigType.

Bicep Parameter Files

Bicep parameter files allow for providing values for parameters in a Bicep template during deployment. For example, in themain.dev.bicepparam file below, we provide a value for the storageAccountConfig parameter that we defined in our Bicep template.

using 'main.bicep'

param storageAccountConfig = {
name: 'sademobicepparamdev001'
sku: 'Standard_LRS'
}

When deploying our Bicep template , we can specify this parameter file to provide the necessary parameter values. This allows for separating environment-specific parameter values from the Bicep template code, which can make managing deployments across different environments easier. Also, the syntax is cleaner and less verbose with json parameters:

Bicep parameters file vs JSON parameters file
$rgName = '<rg_name>'
az deployment group create --resource-group $rgName `
--template-file .\main.bicep `
--parameters .\main.dev.bicepparam

Conclusion

User-defined data types and Bicep parameter files are two powerful features of Bicep that can help make your Bicep code more readable, easier to maintain, and more flexible. By defining your own data types and using parameter files to manage environment-specific parameter values, you can create more robust and easier-to-deploy Bicep templates across different environments.

--

--

Ousmane Barry
Ousmane Barry

Written by Ousmane Barry

Azure Solutions Architect. Coding most of the time with .NET C#.

No responses yet