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
- 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.
How it works
ARM template structure
An ARM template is a JSON file with these main sections.
| Section | Purpose |
|---|---|
| $schema and contentVersion | The template language version and your own version |
| parameters | Values supplied at deployment time |
| variables | Values worked out inside the template |
| resources | The resources to create, each with a type, API version, name and properties |
| outputs | Values 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
paramdeclares an input,vara calculated value andoutputa returned value.resourcedeclares a resource, with a symbolic name, the type and API version.modulecalls another Bicep file so that you can reuse it.targetScopesets 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
| Mode | Behaviour |
|---|---|
| Incremental | The default. Adds and updates resources in the template and leaves other resources in the group untouched |
| Complete | Makes 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.
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
| Function | Use |
|---|---|
| resourceGroup().location | The 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 interpolation | Building names from parts |
| reference | Reading 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 decompilegoes from ARM to Bicep.az bicep buildgoes from Bicep to ARM.
"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.