Bicep is Microsoft's domain-specific language (DSL) for deploying Azure resources declaratively. It provides a transparent abstraction over ARM templates with improved authoring experience, modularity, and enhanced type safety.
Getting Started
Installation
Install the Bicep CLI using Azure CLI:
# Install Bicep tools
az bicep install
# Verify installation
az bicep version
For VS Code users, install the for syntax highlighting, validation, and IntelliSense.
The following example shows a Bicep file that creates a storage account and deploys a web app module:
metadata description = 'Creates a storage account and a web app'
@description('The prefix to use for the storage account name.')
@minLength(3)
@maxLength(11)
param storagePrefix string
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: uniqueStorageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
module webModule './webApp.bicep' = {
name: 'webDeploy'
params: {
skuName: 'S1'
location: location
}
}
Deployment Scopes
Bicep supports various deployment scopes:
// Default is resourceGroup scope
targetScope = 'resourceGroup' // Deploy to a resource group
targetScope = 'subscription' // Deploy to a subscription
targetScope = 'managementGroup' // Deploy to a management group
targetScope = 'tenant' // Deploy to the tenant
Practical Examples
Multi-Region Deployment
// Define regions for deployment
param regions array = [
'eastus'
'westus2'
]
// Deploy resources to each region
@batchSize(1)
module regionDeploy 'region-stack.bicep' = [for region in regions: {
name: 'region-deploy-${region}'
params: {
location: region
environment: environment
}
}]
Secure Parameter Handling
@description('The admin username for the SQL server')
param sqlAdminUsername string
@secure()
@description('The admin password for the SQL server')
param sqlAdminPassword string
resource sqlServer 'Microsoft.Sql/servers@2021-11-01' = {
name: 'sql-${uniqueString(resourceGroup().id)}'
location: location
properties: {
administratorLogin: sqlAdminUsername
administratorLoginPassword: sqlAdminPassword
version: '12.0'
}
}