Deploy and Manage Azure Compute Resources · Topic 1 of 5

ARM Templates and Bicep

AZ-104 deployment: ARM template structure, Bicep syntax, parameters, deployment modes, what-if, exporting a deployment and converting between ARM and Bicep.

  • 5 min
In a nutshell
  • ARM template sections: parameters, variables, resources and outputs.
  • Bicep uses param, var, resource, output and module, and compiles to ARM JSON.
  • Incremental mode is the default. Complete mode deletes resources not in the template.

What it is

Templates let you describe the resources you want in a file and have Azure create them. The same file gives the same result every time, which makes deployments repeatable and reviewable.

Two formats exist. An ARM template is JSON. Bicep is a simpler language that compiles to an ARM template. The exam asks you to read one, change it, deploy it and convert between them.

Bicep or ARM filedescribes resourcesDeploymentto a resource group or subscriptionAzure Resource Managercreates or updates

How it works

ARM template structure

An ARM template is a JSON file with these main sections.

SectionPurpose
$schema and contentVersionThe template language version and your own version
parametersValues supplied at deployment time
variablesValues worked out inside the template
resourcesThe resources to create, each with a type, API version, name and properties
outputsValues returned after deployment

Resources can depend on each other. dependsOn states an order, and Azure also infers dependencies when one resource refers to another.

Bicep

Bicep is a declarative language with less punctuation than JSON. It supports the same features.

param location string = resourceGroup().location

resource sa 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: 'stcontoso001'
  location: location
  sku: { name: 'Standard_LRS' }
  kind: 'StorageV2'
}

output storageId string = sa.id
  • param declares an input, var a calculated value and output a returned value.
  • resource declares a resource, with a symbolic name, the type and API version.
  • module calls another Bicep file so that you can reuse it.
  • targetScope sets the scope of the deployment.

Values can come from a parameter file, either JSON or .bicepparam, so the same template serves several environments.

Deploying

A template can be deployed to a resource group, a subscription, a management group or a tenant.

az deployment group create --resource-group rg-app --template-file main.bicep --parameters location=westeurope
New-AzResourceGroupDeployment -ResourceGroupName rg-app -TemplateFile main.bicep
az deployment group what-if --resource-group rg-app --template-file main.bicep

The what-if command previews the changes without making them.

Deployment modes

ModeBehaviour
IncrementalThe default. Adds and updates resources in the template and leaves other resources in the group untouched
CompleteMakes the group match the template and deletes resources that are not in it

Exporting and converting

You can export a resource group, or a past deployment, as an ARM template from the portal. The export is a starting point and may need cleaning up.

az bicep decompile --file main.json
az bicep build --file main.bicep

decompile converts an ARM template to Bicep on a best effort basis. build converts a Bicep file to an ARM template.

Existing resourcesin a resource groupexportARM templateJSONdecompileBicep fileedit and reuse

Parameters in a template

A parameter file keeps the values separate from the template, so one template serves test and production.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": { "location": { "value": "westeurope" } }
}

Useful template functions

FunctionUse
resourceGroup().locationThe location of the group the template is deployed to
uniqueString(resourceGroup().id)A repeatable string for globally unique names, such as storage accounts
concat or string interpolationBuilding names from parts
referenceReading properties of another resource

Checking and troubleshooting a deployment

Before deploying, validate the template and run what-if. After a failed deployment, open the deployment history on the resource group, or use the CLI.

az deployment group show --resource-group rg-app --name main
az deployment operation group list --resource-group rg-app --name main

The operations list shows which resource failed and why. Remember that the Activity log records the same errors.

Reusing templates

A Bicep module is a file that another file calls, so that a common pattern such as a storage account is written once. For ARM JSON the same idea is a linked template. Both keep large deployments manageable.

How the exam asks about this

  • "Deploy the same environment repeatedly." An ARM template or Bicep file.
  • "Remove anything not in the file." Complete mode.
  • "Preview what a deployment will change." what-if.
  • "Convert a template into Bicep." az bicep decompile.
  • "Export what already exists." Export template from the resource group.

What to remember for the exam

  • ARM template sections: parameters, variables, resources and outputs.
  • Bicep uses param, var, resource, output and module, and compiles to ARM JSON.
  • Incremental mode is the default. Complete mode deletes resources not in the template.
  • what-if previews a deployment.
  • Templates deploy to a resource group, subscription, management group or tenant.
  • az bicep decompile goes from ARM to Bicep. az bicep build goes from Bicep to ARM.
Exam tip

"Deploy and remove anything not in the file" points to Complete mode. "Reuse a block in many templates" points to a Bicep module.

Common mistakes

Using Complete mode on a shared resource group. It deletes everything that is not in the template.

Assuming the decompile is perfect. It is best effort and the result needs review.

Hard coding names and locations. Use parameters so the file works in every environment.

Forgetting the scope. Deploying a subscription level template needs the subscription deployment command.

Check yourself

  • Which deployment mode deletes resources that are not in the template?

    Complete mode.

  • Which command converts an ARM template into a Bicep file?

    az bicep decompile.

  • What does the what-if command do?

    Shows what a deployment would change, without changing anything.

Topic 1 of 5 in Deploy and Manage Azure Compute Resources

Test yourself on Deploy and Manage Azure Compute Resources

The full Microsoft AZ-104 question bank has 650 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.