This blog post will show how to use modules with Bicep and create efficiencies within our code.

Modules allow us to reuse code and create efficiencies in our deployment. Repeatable tasks that create specific resources can group into modules.

Create a Module

To use Bicep modules, we need to create a directory called modules (relative path) and create our module file inside.

A module file is a standard Bicep template with a code referencing a module.

In the code example below, I call a module that creates a storage account using the module keyword. The file name is called file.bicep

I’m also using a few parameters to set the storage account name, and in the last line, I’m outputting the storage account name.


param location string = resourceGroup().location
param utc string = utcNow()
param storageaccountname string = 'ntweekly${uniqueString(utc)}'

module storage 'modules/storage.bicep' = {
  name: storageaccountname
  params: {
    location: location
  
  }
}

output storage string = storage.outputs.storagedetails

The code below shows the actual module that creates the storage account. The module location is modules/storage.bicep.


param location string = resourceGroup().location
param utc string = utcNow()
param storageaccountname string = 'ntweekly${uniqueString(utc)}'

resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: storageaccountname
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }

 }
output storagedetails string = storageAccount.location

Once the module is done, we run the template normally.


Posted

in

by