Azure Bicep: Modules for Flexible Resource Management

Introduction

Hello everyone! We’ve been on an exciting journey exploring Azure Bicep. We’ve seen what it is and how it compares to ARM templates, set up our environment, dived into its syntax, and explored parameters, resources, variables, and outputs. Today, we’re going to take another step forward. We’ll explore one of the key components of Azure Bicep - Modules. So, let’s get started!

Logging in to Azure

Before we start, let’s ensure we’re logged into Azure. Here’s how you can do it:

  1. Open your terminal or command prompt: You can use any terminal or command prompt that you’re comfortable with.
  2. Log in to Azure: Use the az login command to log in to Azure.
    # Log in to Azure
    az login
    
  3. Set your subscription: Use the az account set command to set your subscription.
    # Set your subscription
    az account set --subscription "YourSubscriptionName"
    

Azure Bicep Modules

Modules in Azure Bicep are like functions in a programming language. They allow you to encapsulate a set of resources into a reusable component. This makes your Bicep files more flexible and reusable.

You can define a module using the module keyword. Here’s an example.

module storageAccount './storageAccount.bicep' = {
  name: 'storageAccountModule'
  params: {
    storageAccountName: 'mystorageaccount'
  }
}

In this example, storageAccount is a module that references the storageAccount.bicep file. The name property of the module is set to ‘storageAccountModule’, and the params property is used to pass parameters to the module.

Here’s an example of a storage account. bicep file that can be used as a module.

param storageAccountName string

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: storageAccountName
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}

In this example, the storage Account Name parameter is passed from the main Bicep file to the module.

Conclusion

Well done! You’ve just learned how to use modules in Azure Bicep. Modules are a powerful feature that allows you to encapsulate a set of resources into a reusable component. In our next session, we’ll explore functions in Azure Bicep. So, stay tuned and keep learning