How to Use Bicep for Azure Deployment: Simple Guide
Use
Bicep to define Azure infrastructure as code with simple syntax, then deploy it using az deployment commands. Write your resources in a .bicep file, compile if needed, and run deployment commands to create or update Azure resources.Syntax
Bicep files use a simple, readable syntax to declare Azure resources. You define parameters, variables, and resources with clear keywords. The main parts are:
- param: input values for deployment
- var: variables to simplify expressions
- resource: Azure resources to create or update
- output: values returned after deployment
Each resource has a type, name, API version, and properties.
bicep
param location string = 'eastus' resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = { name: 'mystorageacct123' location: location sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: {} } output storageId string = storageAccount.id
Example
This example creates an Azure Storage Account in the East US region. It shows how to define a parameter for location, declare the storage account resource, and output its resource ID.
To deploy, save this as main.bicep and run the deployment command in Azure CLI.
bicep
param location string = 'eastus' resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = { name: 'mystorageacct123' location: location sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: {} } output storageId string = storageAccount.id
Output
Deployment succeeded.
Outputs:
storageId: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/mystorageacct123
Common Pitfalls
Common mistakes when using Bicep include:
- Using invalid resource names that do not follow Azure naming rules.
- Forgetting to specify the correct API version for resources.
- Not providing required parameters or using wrong types.
- Trying to deploy without logging into Azure CLI or selecting the right subscription.
- Using hardcoded names that cause conflicts if resources already exist.
Always validate your Bicep file with az bicep build and test deployments in a safe environment.
bicep
/* Wrong: Missing API version */ resource storageAccount 'Microsoft.Storage/storageAccounts' = { name: 'badname' location: 'eastus' sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: {} } /* Right: Include API version */ resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = { name: 'goodname' location: 'eastus' sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: {} }
Quick Reference
Here is a quick summary of key Bicep commands for deployment:
bash
az bicep build --file main.bicep az deployment group create --resource-group myResourceGroup --template-file main.bicep az deployment sub create --location eastus --template-file main.bicep
| Command | Description |
|---|---|
| az bicep build --file | Compile Bicep file to ARM JSON template |
| az deployment group create --resource-group | Deploy Bicep to a resource group |
| az deployment sub create --location | Deploy Bicep at subscription scope |
Key Takeaways
Write Azure resources in simple Bicep syntax using parameters, resources, and outputs.
Deploy Bicep files with Azure CLI commands like az deployment group create.
Always specify resource API versions and valid names to avoid deployment errors.
Use az bicep build to validate and compile your Bicep files before deployment.
Test deployments in a safe environment to catch mistakes early.