Bicep - Do you use User-defined Data Types?

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

123
<introEmbed body={<> [User-defined data types in Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/user-defined-data-types?WT.mc_id=DP-MVP-33518) 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

Rick Su
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star