### Classic MSBuild Project File with Bicep Integration
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/msbuild-bicep-file
This example shows a classic MSBuild project file configuration that includes Bicep and Bicep parameter files. Use this example if other methods don't work for your project setup. Ensure placeholder values like ProjectGuid are replaced with unique identifiers.
```xml
Debug
AnyCPU
{11111111-1111-1111-1111-111111111111}
Exe
ClassicFramework
ClassicFramework
v4.8
512
true
true
AnyCPU
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4
AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4
__LATEST_VERSION__
__LATEST_VERSION__
```
--------------------------------
### Example of using a public module
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-using
An example demonstrating how to use a public module with the 'using' statement.
```bicep
using 'br/public:avm/res/storage/storage-account:0.9.0'
param name = 'mystorage'
```
--------------------------------
### Example of using a private module
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-using
An example demonstrating how to use a private module with the 'using' statement.
```bicep
using 'br:myacr.azurecr.io/bicep/modules/storage:v1'
```
--------------------------------
### Get Deployment Script with ARMClient
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deployment-script-bicep
Example of using ARMClient to retrieve a deployment script. Note that ARMClient is not a supported Microsoft tool.
```powershell
armclient login
armclient get /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/myrg/providers/microsoft.resources/deploymentScripts/myDeployementScript?api-version=2020-10-01
```
--------------------------------
### Bicep GUID Function Examples
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-string
Demonstrates creating GUIDs scoped to subscription, resource group, and deployment using the guid function.
```bicep
output guidPerSubscription string = guid(subscription().subscriptionId)
output guidPerResourceGroup string = guid(resourceGroup().id)
output guidPerDeployment string = guid(resourceGroup().id, deployment().name)
```
--------------------------------
### Example of using a template spec
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-using
An example demonstrating how to use a template spec.
```bicep
using 'ts:00000000-0000-0000-0000-000000000000/myResourceGroup/storageSpec:1.0'
```
--------------------------------
### Example of using a private module with an alias
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-using
An example demonstrating how to use a private module with an alias.
```bicep
using 'br/storageModule:storage:v1'
```
--------------------------------
### Simple Module Deployment Example in Bicep
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/modules
A practical example demonstrating how to deploy a module, referencing a local Bicep file for storage account creation. Ensure the referenced file exists.
```bicep
module stgModule '../storageAccount.bicep' = {
name: 'storageDeploy'
params: {
storagePrefix: 'examplestg1'
}
}
```
--------------------------------
### Compile Bicep file using .NET Client Library
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
This example demonstrates using the Azure.Bicep.RpcClient NuGet package to download a specific Bicep CLI version, compile a Bicep file, and output the ARM template. Ensure the Bicep.RpcClient package is installed.
```C#
using Bicep.RpcClient;
var factory = new BicepClientFactory();
using var bicep = await factory.Initialize(new() {
BicepVersion = "0.39.26"
});
var version = await bicep.GetVersion();
Console.WriteLine($"Bicep version: {version}");
var tempFile = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.bicep");
File.WriteAllText(tempFile, """
param foo string
output foo string = foo
""");
var result = await bicep.Compile(new(tempFile));
Console.Write(result.Contents);
```
--------------------------------
### Example Bicep File Implementation
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/file
This example demonstrates the implementation of various Bicep file elements, including metadata, parameters, variables, resources, and modules. It shows how to define a storage account and a web app.
```bicep
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@2025-06-01' = {
name: uniqueStorageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
module webModule './webApp.bicep' = {
name: 'webDeploy'
params: {
skuName: 'S1'
location: location
}
}
```
--------------------------------
### Install Bicep CLI on Linux
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install
Installs the latest Bicep CLI binary, makes it executable, and adds it to the system's PATH.
```bash
# Fetch the latest Bicep CLI binary
curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
# Mark it as executable
chmod +x ./bicep
# Add bicep to your PATH (requires admin)
sudo mv ./bicep /usr/local/bin/bicep
# Verify you can now access the 'bicep' command
bicep --help
# Done!
```
--------------------------------
### Install Bicep MCP Server using dnx
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-mcp-server
Use the dnx command to get the latest version of the Bicep MCP server from the NuGet package.
```bash
dnx -y Azure.Bicep.McpServer
```
--------------------------------
### Example: Publish Bicep Module using Bicep CLI
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli
This is an example of publishing a Bicep module named 'storage.bicep' to a specific registry path and tag, including a documentation URI, using the Bicep CLI.
```bash
bicep publish storage.bicep --target br:exampleregistry.azurecr.io/bicep/modules/storage:v1 --documentationUri https://www.contoso.com/exampleregistry.html
```
--------------------------------
### Base parameter file example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-extend
This is a base parameter file that defines default values for parameters.
```bicep
using none
param app = {
name: 'demo'
tags: {
owner: 'platform'
environment: 'dev'
}
}
param locations = ['westus', 'eastus']
```
--------------------------------
### Example: Publish Bicep Module using Azure CLI
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli
This is an example of publishing a Bicep module named 'storage.bicep' to a specific registry path and tag, including a documentation URI, using the Azure CLI.
```bash
az bicep publish --file storage.bicep --target br:exampleregistry.azurecr.io/bicep/modules/storage:v1 --documentationUri https://www.contoso.com/exampleregistry.html
```
--------------------------------
### Install Bicep CLI on Windows via Winget
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install
Installs the Bicep CLI using the Winget package manager on Windows.
```powershell
winget install -e --id Microsoft.Bicep
```
--------------------------------
### Example what-if output
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-what-if
This is an example of the text output from the what-if operation, indicating resource and property changes with symbols like '-', '+', and '~'. It helps visualize the impact of a deployment before it happens.
```text
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
- tags.Owner: "Team A"
+ properties.enableVmProtection: false
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
properties.defaultOutboundAccess: "Disabled"
properties.privateEndpointNetworkPolicies: "Disabled"
properties.privateLinkServiceNetworkPolicies: "Enabled"
]
Resource changes: 1 to modify.
```
--------------------------------
### Example JSON-RPC Request: bicep/version
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
Demonstrates how to construct a JSON-RPC request to call the 'bicep/version' method. This example shows the required 'jsonrpc', 'id', 'method', and 'params' fields.
```json
Content-Length: 72\r\n\r\n{\"jsonrpc\": \"2.0\", \"id\": 0, \"method\": \"bicep/version\", \"params\": {}}\r\n\r\n
```
--------------------------------
### Connect Bicep CLI via TCP Socket (Example)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
Example of connecting the Bicep CLI to a TCP socket on a specific port.
```Bicep
bicep jsonrpc --socket 12345
```
--------------------------------
### Example Bicep Module Declaration
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli
This is an example of a Bicep module declaration that links to a registry. This file can be used with the `restore` command to download the specified module.
```bicep
module stgModule 'br:exampleregistry.azurecr.io/bicep/modules/storage:v1' = {
name: 'storageDeploy'
params: {
storagePrefix: 'examplestg1'
}
}
```
--------------------------------
### managementGroupResourceId Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-resource
This example demonstrates how to use the managementGroupResourceId function to get the resource ID for a policy definition deployed at the management group scope. It creates a policy definition and then assigns it using the generated ID.
```bicep
targetScope = 'managementGroup'
@description('Target Management Group')
param targetMG string
@description('An array of the allowed locations, all other locations will be denied by the created policy.')
param allowedLocations array = [
'australiaeast'
'australiasoutheast'
'australiacentral'
]
var mgScope = tenantResourceId('Microsoft.Management/managementGroups', targetMG)
var policyDefinitionName = 'LocationRestriction'
resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2025-03-01' = {
name: policyDefinitionName
properties: {
policyType: 'Custom'
mode: 'All'
parameters: {}
policyRule: {
if: {
not: {
field: 'location'
in: allowedLocations
}
}
then: {
effect: 'deny'
}
}
}
}
resource location_lock 'Microsoft.Authorization/policyAssignments@2025-03-01' = {
name: 'location-lock'
properties: {
scope: mgScope
policyDefinitionId: managementGroupResourceId('Microsoft.Authorization/policyDefinitions', policyDefinitionName)
}
dependsOn: [
policyDefinition
]
}
```
--------------------------------
### Deploy Storage Account and Output Properties
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-resource
This example deploys a storage account and demonstrates how to output its properties using both the symbolic name and the `reference` function. It also shows how to output top-level properties like name and location.
```bicep
param storageAccountName string = uniqueString(resourceGroup().id)
param location string = resourceGroup().location
resource storageAccount 'Microsoft.Storage/storageAccounts@2025-06-01' = {
name: storageAccountName
location: location
kind: 'Storage'
sku: {
name: 'Standard_LRS'
}
}
output storageObjectSymbolic object = storageAccount.properties
output storageObjectReference object = reference('storageAccount')
output storageName string = storageAccount.name
output storageLocation string = storageAccount.location
```
--------------------------------
### Deploy Storage Account using Public Module
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/modules
Example of deploying a storage account by referencing a specific version from the public module registry.
```bicep
module storage 'br/public:avm/res/storage/storage-account:0.18.0' = {
name: 'myStorage'
params: {
name: 'store${resourceGroup().name}'
}
}
```
--------------------------------
### Get Bicep CLI Version
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
Call the `bicep/version` method to retrieve the semantic version of the installed Bicep CLI. This method requires no parameters.
```JSON
{}
```
```JSON
{
"version": "0.24.211"
}
```
--------------------------------
### Deploy modules to existing resource groups
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/modules
This example deploys storage accounts to two different, pre-existing resource groups. Each module is scoped to its respective resource group using their symbolic names.
```bicep
targetScope = 'subscription'
resource firstRG 'Microsoft.Resources/resourceGroups@2025-04-01' existing = {
name: 'demogroup1'
}
resource secondRG 'Microsoft.Resources/resourceGroups@2025-04-01' existing = {
name: 'demogroup2'
}
module storage1 '../create-storage-account/main.bicep' = {
name: 'westusdeploy'
scope: firstRG
params: {
storagePrefix: 'stg1'
location: 'westus'
}
}
module storage2 '../create-storage-account/main.bicep' = {
name: 'eastusdeploy'
scope: secondRG
params: {
storagePrefix: 'stg2'
location: 'eastus'
}
}
```
--------------------------------
### Get Azure Container Registry Login Server Name with Azure PowerShell
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/private-module-registry
Use this command to retrieve the login server name for your Azure Container Registry, which is required for referencing modules in Bicep files. Ensure you have the Azure PowerShell module installed and are authenticated.
```powershell
Get-AzContainerRegistry -ResourceGroupName "" -Name "" | Select-Object LoginServer
```
--------------------------------
### Generate GUID Scoped to Subscription
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-string
Create a GUID unique to the current subscription using the guid function with subscription().subscriptionId.
```bicep
guid(subscription().subscriptionId)
```
--------------------------------
### Generate GUID Scoped to Resource Group
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-string
Create a GUID unique to the current resource group using the guid function with resourceGroup().id.
```bicep
guid(resourceGroup().id)
```
--------------------------------
### Example Bicep config file with module aliases
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-config-modules
Demonstrates how to define aliases for both Bicep registries and template specs in a bicepconfig.json file.
```json
{
"moduleAliases": {
"br": {
"ContosoRegistry": {
"registry": "contosoregistry.azurecr.io"
},
"CoreModules": {
"registry": "contosoregistry.azurecr.io",
"modulePath": "bicep/modules/core"
}
},
"ts": {
"CoreSpecs": {
"subscription": "00000000-0000-0000-0000-000000000000",
"resourceGroup": "CoreSpecsRG"
}
}
}
}
```
--------------------------------
### Generate GUID Scoped to Deployment
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-string
Create a GUID unique to the current deployment within a resource group using the guid function with resourceGroup().id and deployment().name.
```bicep
guid(resourceGroup().id, deployment().name)
```
--------------------------------
### Define Storage Account, File Service, and File Share
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/child-resource-name-type
This example demonstrates defining a storage account, its file service, and a file share at the root level. The child resources are defined outside their parent resource.
```Bicep
resource storage 'Microsoft.Storage/storageAccounts@2025-06-01' = {
name: 'examplestorage'
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
resource service 'Microsoft.Storage/storageAccounts/fileServices@2025-06-01' = {
name: 'default'
parent: storage
}
resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2025-06-01' = {
name: 'exampleshare'
parent: service
}
```
--------------------------------
### Check Bicep CLI Installation Locations
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/installation-troubleshoot
Use the `where bicep` command in your command prompt to find all installed locations of the Bicep CLI. This helps identify conflicts between manual installations and the Azure CLI managed version.
```bash
where bicep
```
--------------------------------
### Example Deployment Output Structure
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-deployment
This JSON structure represents the output from the `deployment()` function, showing properties like template details and generator information.
```json
{
"name": "deploymentOutput",
"location": "",
"properties": {
"template": {
"contentVersion": "1.0.0.0",
"metadata": {
"_EXPERIMENTAL_WARNING": "This template uses ARM features that are experimental. Experimental features should be enabled for testing purposes only, as there are no guarantees about the quality or stability of these features. Do not enable these settings for any production usage, or your production environment may be subject to breaking.",
"_EXPERIMENTAL_FEATURES_ENABLED": [
"Asserts"
],
"_generator": {
"name": "bicep",
"version": "0.39.26.7824",
"templateHash": "10348958332696598785"
}
}
}
}
}
```
--------------------------------
### Example Managed Resources Output (CLI)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deployment-stacks
This output shows a list of managed resources, identified by their full Azure resource IDs, that are associated with a deployment stack.
```text
...
Resources: /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Network/virtualNetworks/vnetthmimleef5fwk
/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/demoRg/providers/Microsoft.Storage/storageAccounts/storethmimleef5fwk
```
--------------------------------
### Example of using a template spec with an alias
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-using
An example demonstrating how to use a template spec with an alias.
```bicep
using 'ts/myStorage:storageSpec:1.0'
```
--------------------------------
### Deploy a storage account using a template spec module
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/modules
This example demonstrates deploying a storage account by referencing a template spec named 'storageSpec' with version '2.0', using an alias 'ContosoSpecs' for the resource group.
```bicep
module stgModule 'ts/ContosoSpecs:storageSpec:2.0' = {
name: 'storageDeploy'
params: {
storagePrefix: 'examplestg1'
}
}
```
--------------------------------
### Deploy Tenant-Specific Resources with Modules
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/patterns-logical-parameter
Use a module to loop through a list of tenants defined in a parameter. This pattern simplifies the creation of shared and tenant-specific resources, such as databases and custom domains, for each tenant.
```bicep
module tenantResources 'tenant-resources.bicep' = [for tenant in tenants: {
name: 'tenant-${tenant.id}'
params: {
location: location
tenant: tenant
sqlServerName: sqlServerName
frontDoorProfileName: frontDoorProfileName
}
}]
```
--------------------------------
### Bicep And (&&) operator example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/operators-logical
Use the `&&` operator to determine if both values are true. This example shows evaluating parameter values and expressions.
```bicep
param operand1 bool = true
param operand2 bool = true
output andResultParm bool = operand1 && operand2
output andResultExp bool = 10 >= 10 && 5 > 2
```
--------------------------------
### Invalid Parameter Decorator Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp125
This example demonstrates the use of `@export()` which is an invalid decorator for parameters, causing the BCP125 diagnostic.
```Bicep
@export()
param name string
```
--------------------------------
### Retrieve Module Output for Load Balancer
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/outputs
This example demonstrates how to use a module's output (resourceId) to configure a resource, such as setting the public IP address for a load balancer. The module must be defined and its outputs exposed.
```bicep
module publicIP 'modules/public-ip-address.bicep' = {
name: 'public-ip-address-module'
}
resource loadBalancer 'Microsoft.Network/loadBalancers@2025-01-01' = {
name: loadBalancerName
location: location
properties: {
frontendIPConfigurations: [
{
name: 'name'
properties: {
publicIPAddress: {
id: publicIP.outputs.resourceId
}
}
}
]
// ...
}
}
```
--------------------------------
### Bicep Typo Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp082
This example raises the BCP082 diagnostic because 'substirng' is a typo. Fix this by using the correct function name.
```bicep
var prefix = substirng('1234567890', 0, 11)
```
```bicep
var prefix = substring('1234567890', 0, 11)
```
--------------------------------
### Install Bicep CLI on Windows via Chocolatey
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install
Installs the Bicep CLI using the Chocolatey package manager on Windows.
```powershell
choco install bicep
```
--------------------------------
### Deploy initial virtual network using Azure CLI
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-what-if
Use these Azure CLI commands to create a resource group and deploy the initial Bicep file for the virtual network.
```bash
az group create \
--name ExampleGroup \
--location "Central US"
az deployment group create \
--resource-group ExampleGroup \
--template-file "what-if-before.bicep"
```
--------------------------------
### Create Storage Accounts Using Array Elements and Index
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/loops
Use a `for` loop with both the array element and its index to define properties for multiple storage accounts. This allows for dynamic naming and SKU configuration.
```Bicep
param storageAccountNamePrefix string
var storageConfigurations = [
{
suffix: 'local'
sku: 'Standard_LRS'
}
{
suffix: 'geo'
sku: 'Standard_GRS'
}
]
resource storageAccountResources 'Microsoft.Storage/storageAccounts@2025-06-01' = [for (config, i) in storageConfigurations: {
name: '${storageAccountNamePrefix}${config.suffix}${i}'
location: resourceGroup().location
sku: {
name: config.sku
}
kind: 'StorageV2'
}]
```
--------------------------------
### Install Bicep CLI on macOS via Homebrew
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install
Installs the Bicep CLI using the Homebrew package manager on macOS.
```bash
# Add the tap for bicep
brew tap azure/bicep
# Install the tool
brew install bicep
```
--------------------------------
### Get Deployment Snapshot
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/quickstart-create-bicep-use-visual-studio-code-model-context-protocol
This prompt initiates the 'Get deployment snapshot' command to retrieve a snapshot of the current deployment status.
```plaintext
Get a snapshot of the deployment.
```
--------------------------------
### Bicep Snapshot Example Output
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli
This JSON file shows a normalized snapshot of a Bicep deployment, focusing on the resources themselves by removing module boundaries.
```json
{
"predictedResources": [
{
"id": "[format('/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Storage/storageAccounts/stmyappstorage001', subscription().subscriptionId, resourceGroup().name)]",
"type": "Microsoft.Storage/storageAccounts",
"name": "stmyappstorage001",
"apiVersion": "2025-01-01",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2"
}
],
"diagnostics": []
}
```
--------------------------------
### Invalid Resource Decorator Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp127
This example demonstrates the BCP127 diagnostic. It uses `@export()` which is not a valid resource decorator for resource declarations.
```bicep
@export()
resource store 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: uniqueString(resourceGroup().id)
}
```
--------------------------------
### Deploy a storage account with JSON
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview
This JSON template defines parameters for location and storage account name, then declares a storage account resource with specific SKU, kind, and access tier properties.
```json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"storageAccountName": {
"type": "string",
"defaultValue": "[format('toylaunch{0}', uniqueString(resourceGroup().id))]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-05-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
}
]
}
```
--------------------------------
### Invalid Variable Decorator Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp126
This example demonstrates an invalid variable decorator. The `@minLength()` decorator is not a valid option for Bicep variables.
```bicep
@minLength()
var name = uniqueString(resourceGroup().id)
```
--------------------------------
### Deploy module to a new resource group
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/modules
This example deploys a storage account module to a newly created resource group. The parent Bicep file targets the subscription scope, but the module is explicitly scoped to the `newRG` resource.
```bicep
targetScope = 'subscription'
@minLength(3)
@maxLength(11)
param namePrefix string
param location string = deployment().location
var resourceGroupName = '${namePrefix}rg'
resource newRG 'Microsoft.Resources/resourceGroups@2025-04-01' = {
name: resourceGroupName
location: location
}
module stgModule '../create-storage-account/main.bicep' = {
name: 'storageDeploy'
scope: newRG
params: {
storagePrefix: namePrefix
location: location
}
}
output storageEndpoint object = stgModule.outputs.storageEndpoint
```
--------------------------------
### Install Az Module for PowerShell
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-what-if
Use this command to install or update the Az module in PowerShell, which is required for using the what-if operation.
```PowerShell
Install-Module -Name Az -Force
```
--------------------------------
### Use uri(), uriComponent(), and uriComponentToString()
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-string
Shows how to use the uri() function to construct a URI, then encode it using uriComponent(), and finally decode it back using uriComponentToString().
```bicep
var uriFormat = uri('http://contoso.com/resources/', 'nested/azuredeploy.json')
var uriEncoded = uriComponent(uriFormat)
output uriOutput string = uriFormat
output componentOutput string = uriEncoded
output toStringOutput string = uriComponentToString(uriEncoded)
```
--------------------------------
### Declare storage account resource with placeholders
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/quickstart-create-bicep-use-visual-studio
Initializes a storage account resource with placeholder values for name, location, SKU, and kind.
```bicep
resource exampleStorage 'Microsoft.Storage/storageAccounts@2025-06-01' = {
name: 1
location: 2
sku: {
name: 3
}
kind: 4
}
```
--------------------------------
### Single-Line Comment Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/file
Use '//' for single-line comments in Bicep. This example shows a comment explaining the purpose of a network interface resource.
```bicep
// This is your primary NIC.
resource nic1 'Microsoft.Network/networkInterfaces@2025-01-01' = {
...
}
```
--------------------------------
### Local Bicep Configuration (_bicepconfig.json_)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-config
This is an example of a local _bicepconfig.json_ file, demonstrating how to override default settings like credential precedence and add custom module aliases.
```json
{
"cloud": {
"credentialPrecedence": [
"AzurePowerShell",
"AzureCLI"
]
},
"moduleAliases": {
"br": {
"ContosoRegistry": {
"registry": "contosoregistry.azurecr.io"
},
"CoreModules": {
"registry": "contosoregistry.azurecr.io",
"modulePath": "bicep/modules/core"
}
}
}
}
```
--------------------------------
### Passing Decompiler Cleanup Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter-rule-decompiler-cleanup
This example passes the decompiler cleanup test because the variable names are clear and do not have suffixes indicating decompilation conflicts.
```bicep
var hostingPlanName = functionAppName
var storageAccountName = 'azfunctions${uniqueString(resourceGroup().id)}'
```
--------------------------------
### Azure PowerShell Deployment Script Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deployment-script-bicep-configure-dev
A basic Azure PowerShell script that defines a parameter and outputs a greeting. It demonstrates how to set the $DeploymentScriptOutputs variable for script outputs.
```PowerShell
param([string] $name)
$output = 'Hello {0}' -f $name
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
```
--------------------------------
### Failing example: Missing _artifactsLocationSasToken
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter-rule-artifacts-parameters
This example fails the test because the `_artifactsLocationSasToken` parameter is missing. Ensure both `_artifactsLocation` and `_artifactsLocationSasToken` are provided if one is present.
```bicep
@description('The base URI where artifacts required by this template are located including a trailing \'/\'')
param _artifactsLocation string = deployment().properties.templateLink.uri
...
```
--------------------------------
### Start Bicep CLI with Stdin/Stdout Transport
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
Starts the Bicep CLI's JSON-RPC server using stdin for requests and stdout for responses.
```Bicep
bicep jsonrpc --stdio
```
--------------------------------
### Multiline Comment Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/file
Use '/* ... */' for multiline comments in Bicep. This example provides context for a deployment assuming an existing key vault.
```bicep
/*
This Bicep file assumes the key vault already exists and
is in same subscription and resource group as the deployment.
*/
param existingKeyVaultName string
```
--------------------------------
### Create Multiple Storage Accounts with Integer Index
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/quickstart-loops
Use a for loop with `range()` to create a specified number of storage accounts. The index is used to partially customize the resource name.
```bicep
param rgLocation string = resourceGroup().location
param storageCount int = 2
resource createStorages 'Microsoft.Storage/storageAccounts@2025-06-01' = [for i in range(0, storageCount): {
name: '${i}storage${uniqueString(resourceGroup().id)}'
location: rgLocation
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}]
output names array = [for i in range(0,storageCount) : {
name: createStorages[i].name
} ]
```
--------------------------------
### Failing example: Virtual Machine with literal adminPassword
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter-rule-use-secure-value-for-secure-inputs
This example fails the linter rule because the `adminPassword` is a literal string. Use a secure parameter instead.
```bicep
resource ubuntuVM 'Microsoft.Compute/virtualMachines@2025-04-01' = {
name: 'name'
location: 'West US'
properties: {
osProfile: {
computerName: 'computerName'
adminUsername: 'adminUsername'
adminPassword: 'adminPassword'
}
}
}
```
--------------------------------
### Generate Storage Account SAS Token with Parameters
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-resource
This example shows how to use the `listAccountSas` function, which accepts parameters. It demonstrates passing an object for the expiry time to generate a SAS token for a storage account.
```Bicep
param accountSasProperties object {
default: {
signedServices: 'b'
signedPermission: 'r'
signedExpiry: '2020-08-20T11:00:00Z'
signedResourceTypes: 's'
}
}
...
sasToken: storageAccount.listAccountSas('2021-04-01', accountSasProperties).accountSasToken
```
--------------------------------
### Preview Subscription Deployment Changes (Azure CLI)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-what-if
Use `az deployment sub what-if` to preview changes at the subscription level. Add `--no-pretty-print` for JSON output.
```bash
az deployment sub what-if --location --template-file
```
```bash
az deployment sub what-if --location --template-file --no-pretty-print
```
--------------------------------
### ARM Template JSON Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/decompile
This is an example of an ARM template in JSON format, which can be decompiled into a Bicep file. It defines parameters, variables, resources, and outputs.
```json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS",
"Premium_LRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat('store', uniquestring(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-06-01",
"name": "[variables('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[variables('storageAccountName')]"
}
}
}
```
--------------------------------
### Storage Account Deployment (New or Existing)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/file
This snippet demonstrates a conditional deployment for a storage account, checking if a new one should be created based on the 'newOrExisting' variable.
```bicep
resource sa 'Microsoft.Storage/storageAccounts@2025-06-01' = if (newOrExisting == 'new') {
...
}
```
--------------------------------
### Get Bicep Deployment Graph
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli-jsonrpc
Analyze a Bicep file to get its deployment graph, showing resources and their dependencies. Provide the file path in the params.
```json
{
"path": "/path/to/main.bicep"
}
```
```json
{
"nodes": [
{
"range": { "start": { "line": 2, "char": 0 }, "end": { "line": 8, "char": 1 } },
"name": "storageAccount",
"type": "Microsoft.Storage/storageAccounts",
"isExisting": false,
"relativePath": null
}
],
"edges": [
{ "source": "roleAssignment", "target": "storageAccount" }
]
}
```
--------------------------------
### Bicep Storage Account Declaration Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/resource-declaration
An example of declaring a storage account resource in Bicep. Ensure you use the correct resource type and API version.
```bicep
resource stg 'Microsoft.Storage/storageAccounts@2025-06-01' = {
...
}
```
--------------------------------
### View what-if results with full resource payloads
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-what-if
This example shows the detailed output of a what-if operation when using the FullResourcePayloads format, indicating specific property changes for resources.
```powershell
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
~ Microsoft.Network/virtualNetworks/vnet-001 [2018-10-01]
- tags.Owner: "Team A"
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
]
Resource changes: 1 to modify.
```
--------------------------------
### Bicep: Using outputted resource ID in a subsequent deployment
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter-rule-outputs-should-not-contain-secrets
This example demonstrates how the outputted resource ID from a module can be used to retrieve sensitive information, such as storage account keys, in a later deployment step.
```bicep
someProperty: listKeys(myStorageModule.outputs.storageId.value, '2021-09-01').keys[0].value
```
--------------------------------
### Failing example: VM Scale Set with literal adminPassword
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/linter-rule-use-secure-value-for-secure-inputs
This example fails the linter rule because the `adminPassword` is a literal string. Use a secure parameter instead.
```bicep
resource ubuntuVM 'Microsoft.Compute/virtualMachineScaleSets@2025-04-01' = {
name: 'name'
location: 'West US'
properties: {
virtualMachineProfile: {
osProfile: {
adminUsername: 'adminUsername'
adminPassword: 'adminPassword'
}
}
}
}
```
--------------------------------
### Invalid Data Type Example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp302
This example raises the BCP302 diagnostic because 'balla' is an invalid type, likely a typo. Ensure you use valid Bicep types.
```bicep
type ball = {
name: string
color: string
}
output tennisBall balla = {
name: 'tennis'
color: 'yellow'
}
```
--------------------------------
### Dynamic Network Security Group Outputs
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/outputs
This example demonstrates creating multiple network security group resources and then outputting their details by iterating over an array of organization names. It shows how to dynamically generate outputs based on a collection.
```bicep
param nsgLocation string = resourceGroup().location
param orgNames array = [
'Contoso'
'Fabrikam'
'Coho'
]
resource nsg 'Microsoft.Network/networkSecurityGroups@2025-01-01' = [for name in orgNames: {
name: 'nsg-${name}'
location: nsgLocation
}]
output deployedNSGs array = [for (name, i) in orgNames: {
orgName: name
nsgName: nsg[i].name
resourceId: nsg[i].id
}]
```
--------------------------------
### Invalid output decorator example
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/diagnostics/bcp129
This example demonstrates an invalid output decorator (`@export()`) which will raise the BCP129 diagnostic. Use valid decorators for output declarations.
```bicep
@export()
output foo string = 'Hello world'
```
--------------------------------
### Deploy Bicep with inline parameters from files (Bash)
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-cli
Provide parameter values by referencing local files using the '@' prefix. This is useful for complex configurations or sensitive data.
```bash
az deployment group create \
--resource-group testgroup \
--template-file \
--parameters exampleString=@stringContent.txt exampleArray=@arrayContent.json
```
--------------------------------
### Install Specific Bicep CLI Version
Source: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-cli
Installs a specific version of the Bicep CLI. Use this command when you need to pin to a particular Bicep CLI version.
```bash
az bicep install --version v0.37.4
```