Create Helper Modules with Azure Bicep
What is Azure Bicep?
Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure resources. It provides concise syntax, reliable type safety, and support for code reuse.
// storage-account.bicep
@description('Optional. The location of the resource to be deployed in to.')
param location string = resourceGroup().location
@description('Required. The name of the Storage Account resource.')
param storageAccountName string
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-06-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
accessTier: 'Hot'
}
}
A Bicep resource that deploys a standard Azure Storage account.
What are Bicep modules?
Modules are Bicep files deployed from another Bicep file. They make your code easier to read by wrapping up the details of a deployment, and they let you reuse code across deployments. The documentation covers them in depth.
// main.bicep
var storageAccountName = 'toylaunch${uniqueString(resourceGroup().id)}'
module storageAccountModule '../storage-account.bicep' = {
name: 'storageDeploy'
params: {
storageAccountName: storageAccountName
}
}
The basic syntax for a Storage Account module. The snippet uses the resource from storage-account.bicep directly.
Why should I use modules?
Modules make your files easier to read, easier to reuse and flatter. They also let you create helper modules. A helper module is a Bicep module that does not deploy any Azure resources, but it can assist you with repetitive tasks where you may need formatted values.
This helps when you need the same values throughout your code base, such as IP addresses, dates and resource naming conventions. These values need a certain format, and the more your code base grows, the more places need them.
Problem
Take IP addresses as an example. Each resource expects them in its own format, which makes infrastructure harder to manage. An incorrect format can result in deployment errors or connectivity issues, causing potential downtime. For example:
module storageAccountModule '../storage-account.bicep' = {
name: 'storageDeploy'
params: {
storageAccountName: storageAccountName
networkAcls: {
defaultAction: 'deny'
// ipRules: IPRule[]
ipRules: [
{
action: 'Allow'
value: '192.0.2.0/24'
}
]
}
}
}
The IPRule arrays are made up of objects with two values inside: action and value. Action is the action of the IP rule which has a type of 'Allow' | 'Deny', and value specifies the IP or IP range in CIDR format.
That configuration is valid for storage accounts, but it doesn't work for Cosmos DB:
// main.bicep
module cosmos '../cosmos.bicep' = {
name: 'cosmosDeploy'
params: {
cosmosAccountName: cosmosAccountName
// ipRules: IPRule[]
ipRules: [
{
ipAddressOrRange: '192.0.2.0/24'
}
]
}
}
Here, ipAddressOrRange is the only valid field in IPRule.
Solution
To avoid misconfigurations or a large variable containing all IP addresses at the top of all your Bicep files, you can make use of a helper module.
// ip-range-helper.bicep
var whiteListedIps = [
{
Name: 'Rule1'
CIDR: '192.0.2.0/24'
StartIP: '192.0.2.0'
EndIP: '192.0.2.255'
}
]
var cosmosFormat = [for address in whiteListedIps: {
ipAddressOrRange: address.CIDR
}]
var storageFormat = [for address in whiteListedIps: {
value: address.CIDR
action: 'Allow'
}]
output cosmosFormat array = cosmosFormat
output storageFormat array = storageFormat
Now to consume this module:
// main.bicep
module ipRangeHelper '../ip-range-helper.bicep' = {
name: 'ip-ranges'
}
module storageAccountModule '../storage-account.bicep' = {
name: 'storageDeploy'
params: {
storageAccountName: storageAccountName
networkAcls: {
defaultAction: 'deny'
ipRules: ipRangeHelper.outputs.storageFormat
}
}
}
module cosmos '../cosmos.bicep' = {
name: 'cosmosDeploy'
params: {
cosmosAccountName: cosmosAccountName
ipRules: ipRangeHelper.outputs.cosmosFormat
}
}
Summary
Bicep modules make code easier to read and reuse, and they are also a good tool for repetitive tasks and for keeping your infrastructure consistent. A helper module lets you define a format once and use it everywhere.