Bird
Raised Fist0
Azurecloud~5 mins

Bicep as ARM simplification in Azure - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Deploying resources in Azure can be complex using raw ARM templates because they use long JSON files. Bicep simplifies this by using a cleaner, easier language that compiles into ARM templates, making deployments faster and less error-prone.
When you want to deploy Azure resources with simpler and more readable code than raw ARM JSON.
When you need to reuse parts of your infrastructure code with modules.
When you want to avoid manual JSON editing and reduce syntax errors.
When you want to quickly test and deploy infrastructure changes in Azure.
When you want to maintain infrastructure as code with better tooling support.
Config File - main.bicep
main.bicep
param location string = 'eastus'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: 'examplestorageacct'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {}
}

output storageAccountId string = storageAccount.id

This Bicep file defines a storage account resource in Azure.

param location: sets the Azure region for deployment.

resource storageAccount: declares a storage account with a name, location, SKU, and kind.

output storageAccountId: outputs the resource ID after deployment.

Commands
This command compiles the Bicep file into an ARM JSON template that Azure understands for deployment.
Terminal
az bicep build --file main.bicep
Expected OutputExpected
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location": { "type": "string", "defaultValue": "eastus" } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2022-09-01", "name": "examplestorageacct", "location": "[parameters('location')]", "sku": { "name": "Standard_LRS" }, "kind": "StorageV2", "properties": {} } ], "outputs": { "storageAccountId": { "type": "string", "value": "[resourceId('Microsoft.Storage/storageAccounts', 'examplestorageacct')]" } } }
--file - Specifies the Bicep file to compile
This command deploys the compiled ARM template to the specified Azure resource group.
Terminal
az deployment group create --resource-group example-rg --template-file main.json
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Resources/deployments/mainDeployment", "name": "mainDeployment", "properties": { "provisioningState": "Succeeded", "outputs": { "storageAccountId": { "type": "String", "value": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/examplestorageacct" } } } }
--resource-group - Specifies the target Azure resource group
--template-file - Specifies the ARM template file to deploy
This command checks the status and details of the deployment to confirm it succeeded.
Terminal
az deployment group show --resource-group example-rg --name mainDeployment
Expected OutputExpected
{ "name": "mainDeployment", "properties": { "provisioningState": "Succeeded", "outputs": { "storageAccountId": { "type": "String", "value": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/examplestorageacct" } } } }
--resource-group - Specifies the resource group of the deployment
--name - Specifies the deployment name to query
Key Concept

If you remember nothing else from this pattern, remember: Bicep lets you write simple, clean code that turns into ARM templates for easy Azure resource deployment.

Common Mistakes
Trying to deploy the Bicep file directly without compiling it first.
Azure only accepts ARM JSON templates for deployment, so the Bicep file must be compiled first.
Always run 'az bicep build --file main.bicep' to create the ARM template before deploying.
Using an incorrect resource group name or one that does not exist during deployment.
Deployment fails if the resource group does not exist or is misspelled.
Create the resource group first with 'az group create' or verify the name before deploying.
Not specifying the location parameter or using an invalid Azure region.
The deployment will fail or resources will be created in an unintended region.
Set a valid Azure region like 'eastus' in the parameter or use the default.
Summary
Write infrastructure code in Bicep for simpler and cleaner Azure resource definitions.
Compile Bicep files into ARM JSON templates using 'az bicep build'.
Deploy the compiled ARM template to Azure with 'az deployment group create'.
Check deployment status with 'az deployment group show'.

Practice

(1/5)
1. What is the main purpose of Bicep in Azure infrastructure management?
easy
A. To monitor Azure resources in real-time
B. To replace Azure CLI commands
C. To create virtual machines automatically
D. To simplify writing and managing Azure resource templates

Solution

  1. Step 1: Understand Bicep's role

    Bicep is designed to simplify the process of writing infrastructure as code for Azure by providing a cleaner syntax than raw ARM templates.
  2. Step 2: Compare with other options

    Options A, B, and C describe other Azure tools or features, not Bicep's main purpose.
  3. Final Answer:

    To simplify writing and managing Azure resource templates -> Option D
  4. Quick Check:

    Bicep simplifies Azure templates = D [OK]
Hint: Bicep makes Azure templates easier to write and read [OK]
Common Mistakes:
  • Confusing Bicep with Azure CLI
  • Thinking Bicep creates resources automatically
  • Assuming Bicep monitors resources
2. Which of the following is the correct way to declare a string parameter named location in a Bicep file?
easy
A. param location string
B. parameter location = 'string'
C. var location string
D. resource location string

Solution

  1. Step 1: Recall Bicep parameter syntax

    In Bicep, parameters are declared using the keyword param followed by the name and type, e.g., param location string.
  2. Step 2: Eliminate incorrect options

    parameter location = 'string' uses incorrect syntax; var location string uses var which is for variables, not parameters; resource location string uses resource which is for resources.
  3. Final Answer:

    param location string -> Option A
  4. Quick Check:

    Parameter declaration = param name type [OK]
Hint: Use 'param' keyword for parameters in Bicep [OK]
Common Mistakes:
  • Using 'var' instead of 'param' for parameters
  • Confusing resource declaration with parameter
  • Incorrect assignment syntax for parameters
3. Given this Bicep snippet:
param storageAccountName string = 'mystorage'
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: storageAccountName
  location: 'eastus'
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
}

What will be the name of the deployed storage account?
medium
A. mystorage
B. storageAccountName
C. Standard_LRS
D. eastus

Solution

  1. Step 1: Identify the parameter value

    The parameter storageAccountName is set to the string 'mystorage'.
  2. Step 2: Check resource name assignment

    The resource's name property uses the parameter storageAccountName, so the deployed storage account will be named 'mystorage'.
  3. Final Answer:

    mystorage -> Option A
  4. Quick Check:

    Resource name = parameter value 'mystorage' [OK]
Hint: Resource name uses parameter value directly [OK]
Common Mistakes:
  • Confusing parameter name with its value
  • Choosing SKU or location as name
  • Assuming default resource name
4. You wrote this Bicep code:
param location string
var location = 'westus'
resource vm 'Microsoft.Compute/virtualMachines@2022-03-01' = {
  name: 'myVM'
  location: location
}

What is the error in this code?
medium
A. Missing SKU property in the resource
B. Resource name must be a parameter, not a string
C. Variable and parameter have the same name causing conflict
D. Location property cannot be a variable

Solution

  1. Step 1: Identify naming conflict

    Both a parameter and a variable are named location, which causes a conflict in Bicep because names must be unique in the same scope.
  2. Step 2: Check other options

    Resource name can be a string literal; SKU is not mandatory for all resources; location can be a variable if no conflict exists.
  3. Final Answer:

    Variable and parameter have the same name causing conflict -> Option C
  4. Quick Check:

    Duplicate names cause errors in Bicep [OK]
Hint: Avoid using same name for param and var [OK]
Common Mistakes:
  • Thinking resource name must be a parameter
  • Assuming SKU is always required
  • Believing location cannot be variable
5. You want to deploy two storage accounts in different locations using Bicep. Which approach correctly uses a loop to create these resources?
hard
A. param locations string = 'eastus,westus' resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = { name: 'storage' location: locations kind: 'StorageV2' sku: { name: 'Standard_LRS' } }
B. var locations = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for loc in locations: { name: 'storage${loc}' location: loc kind: 'StorageV2' sku: { name: 'Standard_LRS' } }]
C. var locations = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = { name: 'storage' location: locations kind: 'StorageV2' sku: { name: 'Standard_LRS' } }
D. param locations array = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = for loc in locations { name: 'storage' location: loc kind: 'StorageV2' sku: { name: 'Standard_LRS' } }

Solution

  1. Step 1: Understand Bicep loop syntax

    Bicep uses array loops with the syntax [for item in array: { ... }] to create multiple resources.
  2. Step 2: Analyze each option

    var locations = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for loc in locations: { name: 'storage${loc}' location: loc kind: 'StorageV2' sku: { name: 'Standard_LRS' } }] correctly uses a variable array and a loop to create multiple storage accounts with unique names and locations. Options B and C misuse the location property by assigning an array directly. param locations array = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = for loc in locations { name: 'storage' location: loc kind: 'StorageV2' sku: { name: 'Standard_LRS' } } has incorrect loop syntax missing square brackets.
  3. Final Answer:

    var locations = ['eastus', 'westus'] resource storageAccounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [for loc in locations: { name: 'storage${loc}' location: loc kind: 'StorageV2' sku: { name: 'Standard_LRS' } }] -> Option B
  4. Quick Check:

    Bicep loops use [for item in array: {...}] [OK]
Hint: Use [for item in array: {...}] for resource loops [OK]
Common Mistakes:
  • Assigning array directly to location property
  • Incorrect loop syntax without brackets
  • Using string instead of array for multiple locations