Bicep - Do you use User-defined Data Types?

Updated by Brady Stroud [SSW] 1 year ago. See history

123

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

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

param sku object

❌ Figure: 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
}

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

acknowledgements
related rules