Posted on 3/31/2023
Have you ever needed to share specific Bicep modules with your team or organization? Perhaps you want to reuse certain modules across multiple repositories? Azure Bicep templates offer the functionality to pull module templates from an Azure Container Registry, allowing you to store common modules in a centralized location.
Azure Bicep templates can reference module templates stored in an Azure Container Registry. Here's an example of how to do that:
module myModule 'br:<REPLACE_THIS_WITH_YOUR_ACR_NAME>.azurecr.io/bicep/modules/mymodule:v0.1' = {
scope: rg
name: 'deploymentName'
params: {
....
}
}
By using this approach, you can maintain common modules in a single Azure Container Registry within your organization
There are no specific requirements for the registry. The basic SKU is sufficient for hosting your modules, but any other SKU will work if you already have an existing Container Registry you'd like to reuse.
Use the template below to create a Container Registry:
param name string
param location string
@allowed([
'Basic'
'Standard'
'Premium'
])
param sku string = 'Basic'
resource registry 'Microsoft.ContainerRegistry/registries@2022-02-01-preview' = {
name: name
location: location
sku: {
name: sku
}
properties: {
adminUserEnabled: false
anonymousPullEnabled: false
publicNetworkAccess: Enabled
}
}
Deploy the template using the Azure CLI:
az deployment group create --name ContainerRegistry --resource-group <resource group name> --template-file <path-to-bicep> --parameters name=<name of the container registry> location=<location>```
## Publish a module to the Registry
Publishing a Module to the Registry
After creating the container registry, you'll want to publish a Bicep file to it.
First, log in to the Azure CLI (ensure you have the necessary permissions for the Container Registry):
```sh
az login
Then, publish the Bicep file using the Azure CLI:
az bicep publish --file <your bicep module> --target "br:<container registry name>/bicep/modules/<your module name>:<your version tag>"
By following these steps, you can effectively share and reuse Bicep modules across your organization, streamlining your development process and promoting code consistency.