Bicep - Do you use User-defined Data Types?

Last updated by Brady Stroud [SSW] 17 days ago.See history

User-defined data types in Bicep allow you to create custom data structures for better code organization and type safety. They enhance reusability, abstraction, and maintainability within projects.

When creating a cloud resource, numerous parameters are typically required for configuration and customization. Organizing and naming these parameters effectively is increasingly important.

@allowed(['Basic', 'Standard'])
param skuName string = 'Basic'
@allowed([5, 10, 20, 50, 100])
param skuCapacity int = 5
param skuSizeInGB int = 2

Bad example - Relying on parameter prefixes and order leads to unclear code, high complexity, and increased maintenance effort

param sku object

Bad example - When declaring a parameter as an untyped object, bicep cannot validate the object's properties and values at compile time, risking runtime errors.

// User-defined data type
type skuConfig = {
  name: 'Basic' | 'Standard'
  capacity: 5 | 10 | 20 | 50 | 100
  sizeInGB: int
}

param sku skuConfig = {
  name: 'Basic'
  capacity: 5
  sizeInGB: 2
}

Good example - User-defined data type provides type safety, enhanced readability and making maintenance easier

We open source. Powered by GitHub