You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Modules and loops are essential features for building maintainable, DRY (Don't Repeat Yourself) Bicep templates. Modules break large templates into reusable components, while loops create multiple resources from a single definition.
A module is a Bicep file that is consumed by another Bicep file. Modules enable:
A module is simply a regular Bicep file. For example, a storage account module:
modules/storage.bicep
@description('Name of the storage account')
param storageAccountName string
@description('Azure region')
param location string = resourceGroup().location
@description('Storage SKU')
@allowed(['Standard_LRS', 'Standard_GRS', 'Standard_ZRS'])
param skuName string = 'Standard_LRS'
@description('Tags to apply')
param tags object = {}
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
name: storageAccountName
location: location
sku: {
name: skuName
}
kind: 'StorageV2'
properties: {
minimumTlsVersion: 'TLS1_2'
supportsHttpsTrafficOnly: true
allowBlobPublicAccess: false
}
tags: tags
}
output storageAccountId string = storageAccount.id
output primaryBlobEndpoint string = storageAccount.properties.primaryEndpoints.blob
output name string = storageAccount.name
main.bicep
param environment string = 'dev'
param location string = resourceGroup().location
var commonTags = {
environment: environment
managedBy: 'bicep'
}
module appStorage 'modules/storage.bicep' = {
name: 'appStorageDeployment'
params: {
storageAccountName: 'stapp${environment}${uniqueString(resourceGroup().id)}'
location: location
skuName: environment == 'prod' ? 'Standard_GRS' : 'Standard_LRS'
tags: commonTags
}
}
module logsStorage 'modules/storage.bicep' = {
name: 'logsStorageDeployment'
params: {
storageAccountName: 'stlogs${environment}${uniqueString(resourceGroup().id)}'
location: location
tags: commonTags
}
}
output appStorageEndpoint string = appStorage.outputs.primaryBlobEndpoint
output logsStorageEndpoint string = logsStorage.outputs.primaryBlobEndpoint
The same storage.bicep module is used twice with different parameters.
Modules can deploy resources to different scopes:
module networkModule 'modules/network.bicep' = {
name: 'networkDeployment'
scope: resourceGroup('rg-networking')
params: {
vnetName: 'vnet-shared'
location: location
}
}
targetScope = 'subscription'
module rgModule 'modules/resourceGroup.bicep' = {
name: 'rgDeployment'
scope: subscription()
params: {
rgName: 'rg-webapp-prod'
location: 'uksouth'
}
}
You can publish modules to an Azure Container Registry (ACR) and consume them by reference:
az bicep publish \
--file modules/storage.bicep \
--target br:myregistry.azurecr.io/bicep/modules/storage:v1.0
module appStorage 'br:myregistry.azurecr.io/bicep/modules/storage:v1.0' = {
name: 'appStorageDeployment'
params: {
storageAccountName: 'stapp${uniqueString(resourceGroup().id)}'
location: location
}
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.