0
0
Azurecloud~7 mins

Bicep syntax and modules in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Bicep helps you write Azure infrastructure code simply. It solves the problem of complex JSON templates by using easier syntax. Modules let you reuse parts of your code to keep things neat and organized.
When you want to create Azure resources like storage or virtual machines with simple code.
When you need to reuse the same resource setup in different projects or environments.
When you want to split a big infrastructure setup into smaller, manageable pieces.
When you want to avoid writing long and hard-to-read JSON ARM templates.
When you want to share common infrastructure code with your team.
Config File - main.bicep
main.bicep
param location string = 'eastus'

module storageModule './storage.bicep' = {
  name: 'storageDeployment'
  params: {
    storageName: 'examplestorage123'
    location: location
  }
}

output storageAccountId string = storageModule.outputs.storageAccountId

This is the main Bicep file that deploys a storage account using a module.

param location: sets the Azure region.

module storageModule: calls the storage.bicep file, passing parameters.

output storageAccountId: exports the storage account ID from the module.

Commands
This command deploys the Bicep template to the Azure resource group named example-rg. It creates the resources defined in main.bicep and its modules.
Terminal
az deployment group create --resource-group example-rg --template-file main.bicep
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Resources/deployments/main", "name": "main", "properties": { "provisioningState": "Succeeded", "outputs": { "storageAccountId": { "type": "String", "value": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/examplestorage123" } } } }
--resource-group - Specifies the Azure resource group to deploy to.
--template-file - Specifies the Bicep file to deploy.
This command checks that the storage account was created successfully by showing its details.
Terminal
az storage account show --name examplestorage123 --resource-group example-rg
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/examplestorage123", "location": "eastus", "name": "examplestorage123", "resourceGroup": "example-rg", "sku": { "name": "Standard_LRS" }, "kind": "StorageV2", "statusOfPrimary": "available" }
--name - Specifies the storage account name.
--resource-group - Specifies the resource group where the storage account exists.
Key Concept

If you remember nothing else from this pattern, remember: Bicep modules let you reuse and organize Azure infrastructure code by calling separate files with parameters.

Common Mistakes
Using incorrect file paths for modules in the main Bicep file.
The deployment fails because Bicep cannot find the module file to include.
Ensure the module path is correct relative to the main Bicep file, like './storage.bicep'.
Not passing required parameters to the module.
The module deployment fails due to missing inputs needed to create resources.
Always provide all parameters defined in the module when calling it.
Trying to deploy Bicep files without using the Azure CLI or without compiling.
Azure does not understand Bicep files directly without deployment commands or compilation.
Use 'az deployment group create' with the Bicep file or compile it to ARM JSON before deploying.
Summary
Write reusable infrastructure code in Bicep modules to keep templates clean.
Deploy Bicep files with Azure CLI using 'az deployment group create'.
Verify resource creation with Azure CLI commands like 'az storage account show'.