In this post, we will see what Bicep is and how to use it in our Azure projects.
Azure Bicep is a declarative language built by Microsoft to author Azure infrastructure configurations more intuitively than traditional ARM templates. It’s essentially a higher-level abstraction over ARM JSON, designed to reduce complexity and improve readability while maintaining full compatibility with Azure’s resource management capabilities.
It provides a more concise syntax with improved type safety, modularity, and intellisense support, making it easier to define and deploy Azure infrastructure as code.
Key Features of Bicep:
- Simplified Syntax: Bicep uses a clean, concise syntax, eliminating the verbose JSON structure of ARM templates.
- Modularity: Supports reusable modules, making it easier to organize and share code across projects.
- Type Safety: Provides compile-time validation, catching errors before deployment.
- Native Azure Integration: Bicep compiles directly to ARM JSON, ensuring compatibility with all Azure services and APIs.
- Tooling Support: Integrates seamlessly with Visual Studio Code, offering autocompletion, linting, and IntelliSense.
When to Use Bicep:
- New Projects: Start with Bicep for its simplicity and maintainability, especially for teams new to IaC.
- Complex Deployments: Use Bicep’s modularity for large-scale, multi-resource deployments to improve code reuse.
- Refactoring ARM Templates: Gradually migrate existing ARM templates to Bicep for better readability and error handling.
- Collaboration: Bicep’s intuitive syntax is ideal for teams with mixed skill levels, reducing the learning curve.
- CI/CD Pipelines: Bicep’s integration with Azure CLI and PowerShell makes it a great fit for automated deployment pipelines.
I want to highlight again that Bicep is not a replacement for ARM templates but, a user-friendly layer on top. If we are managing Azure resources and want to reduce boilerplate code, catch errors early, or improve collaboration, Bicep is the go-to choice.
Under the hood, Bicep compiles to exactly the same ARM schema, so we get the same deployment semantics and Azure support.
Finally, while Bicep and ARM templates both define Azure infrastructure, they differ significantly in syntax, usability, and developer experience. Here’s a detailed comparison:
[syntax]
Azure Bicep => Concise, human-readable DSL with minimal boilerplate.
ARM => Verbose JSON with complex structure, prone to errors.
[modularity]
Azure Bicep => Native support for modules, enabling reusable components.
ARM => Limited modularity; requires linked templates, which are harder to manage.
[validation]
Azure Bicep => Compile-time validation with type checking via Bicep CLI.
ARM => Validation only during deployment, increasing risk of runtime errors.
[tooling]
Azure Bicep => Rich VS Code extension with IntelliSense, linting, and formatting.
ARM => Limited tooling; JSON schema validation is less intuitive.
[learning curve]
Azure Bicep => Gentler with more intuitive syntax.
ARM => Steeper due to JSON complexity.
Let’s look at three practical examples that demonstrate Bicep’s capabilities.
Example 1: Creating a Service Bus Topic
This Bicep file creates a Service Bus namespace and a topic named testBiceptopic.
// servicebus.bicep
param location string = resourceGroup().location
param namespaceName string = 'testBicepNamespace'
param topicName string = 'testBiceptopic'
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' = {
name: namespaceName
location: location
sku: {
name: 'Standard'
tier: 'Standard'
}
}
resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2021-11-01' = {
name: '${namespaceName}/${topicName}'
parent: serviceBusNamespace
}
Example 2: Creating a Cosmos DB Account and Database
This Bicep file creates a Cosmos DB account named damianodbaccount with a database named TestCosmos.
// cosmosdb.bicep
param location string = resourceGroup().location
param accountName string = 'damianodbaccount'
param databaseName string = 'TestCosmos'
resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2023-04-15' = {
name: toLower(accountName)
location: location
kind: 'GlobalDocumentDB'
properties: {
consistencyPolicy: {
defaultConsistencyLevel: 'Session'
}
locations: [
{
locationName: location
failoverPriority: 0
}
]
databaseAccountOfferType: 'Standard'
}
}
resource cosmosDbDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2023-04-15' = {
name: '${cosmosDbAccount.name}/${databaseName}'
properties: {
resource: {
id: databaseName
}
}
}
Example 3: Creating a Virtual Network with a Subnet
This example demonstrates a Bicep file for a virtual network (VNet) with a single subnet, showcasing Bicep’s flexibility.
// vnet.bicep
param location string = resourceGroup().location
param vnetName string = 'testBicepVnet'
param subnetName string = 'defaultSubnet'
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-05-01' = {
name: vnetName
location: location
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
}
}
resource subnet 'Microsoft.Network/virtualNetworks/subnets@2023-05-01' = {
name: '${vnetName}/${subnetName}'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
Finally, the steps to deploy a Bicep file:
- Prepare: Ensure an Azure subscription, authenticate via Azure CLI or PowerShell, and create a resource group.
- Create: Save our Bicep file with resource configurations.
- Validate: Compile the Bicep file to check for errors using Azure CLI or PowerShell.
- Deploy: Run the deployment command with Azure CLI or PowerShell, specifying the resource group, Bicep file, and parameters (e.g., location).
- Verify: Check the Azure Portal or list resources to confirm deployment.
Azure Bicep represents a significant improvement over traditional ARM templates for infrastructure as code in Azure. With its cleaner syntax, better tooling, and seamless integration with existing ARM knowledge, it’s an excellent choice for our Azure projects.
For comprehensive documentation and the latest information on Azure Bicep, including detailed deployment options, advanced features, and best practices, visit the official Microsoft Learn documentation at https://learn.microsoft.com/en-gb/azure/azure-resource-manager/bicep/.
This resource is regularly updated and provides everything we need about Bicep deployment and management.