Working with Bicep files at scale

Azure Bicep Creating and Deploying Azure Resources Using main.bicepparam, main.bicep, and resource.bicep Files

Introduction

Azure Bicep is a domain-specific language (DSL) for deploying Azure resources declaratively. It simplifies the authoring experience with a more concise syntax compared to traditional ARM templates. By using Bicep, you can modularize your infrastructure as code (IaC) and manage complex deployments with ease.

Detailed Step-by-Step Implementation Guide

  1. Prerequisites

    • Azure subscription
    • Azure CLI installed
    • Bicep CLI installed
  2. Create the Bicep Files

    • main.bicepparam

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      {
        "$schema": "https://schema.management.azure.com/schemas/2020-10-01/tenantDeploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
          "location": {
            "value": "australiaeast"
          },
          "storageAccountName": {
            "value": "mystorageaccount"
          }
        }
      }
      
    • resource.bicep

      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      resource storageAccount 'Microsoft.Storage/storageAccounts@2021-02-01' = {
        name: storageAccountName
        location: location
        sku: {
          name: 'Standard_LRS'
        }
        kind: 'StorageV2'
        properties: {}
      }
      
    • main.bicep

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      
      param location string
      param storageAccountName string
      
      module storageModule 'resource.bicep' = {
        name: 'storageModule'
        params: {
          location: location
          storageAccountName: storageAccountName
        }
      }
      
  3. Deploy the Bicep Files Using Azure CLI

    • Deploy the main.bicep file with parameters

      1
      
      az deployment group create --resource-group <Your Resource Group> --parameters main.bicepparam
      

Why Split the Files?

Splitting the files into main.bicepparam, main.bicep, and resource.bicep offers several advantages:

  • Modularity: By separating parameters, main logic, and resources, you can reuse and manage each component independently.
  • Readability: Smaller, focused files are easier to read and understand.
  • Maintainability: Changes in one file do not affect others, making updates and debugging simpler.
  • Collaboration: Different team members can work on different parts of the deployment without conflicts.

Conclusion

By following this guide, you have successfully created and deployed Azure resources using Azure Bicep with the main.bicepparam, main.bicep, and resource.bicep files. This modular approach allows for better organisation and management of your infrastructure as code, making deployments more efficient and maintainable.

Learn More