ARM templates and Bicep
Read, modify and deploy infrastructure as code, preview changes safely, and convert between ARM JSON and Bicep.
In this lesson you'll learn
- The sections of an ARM template and what each one does
- The same deployment written in Bicep
- Deploying with CLI and PowerShell, what-if, and Incremental versus Complete mode
- Exporting templates, decompiling to Bicep, and keeping secrets in Key Vault
1. Why infrastructure as code?
New to templates? Start here
Clicking through the portal works once. Doing it identically 50 times does not. A template is a file that describes the resources you want. Azure Resource Manager reads it and builds exactly that, every time. You can store it in Git, review changes and redeploy in minutes.
ARM templates are JSON. Bicep is a cleaner language that compiles into ARM JSON. Both deploy the same way.
2. The same thing in Bicep
param location string = resourceGroup().location
param storageName string
@secure()
param adminPassword string
var skuName = 'Standard_GRS'
resource st 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: storageName
location: location
sku: { name: skuName }
kind: 'StorageV2'
}
output blobEndpoint string = st.properties.primaryEndpoints.blob
- param, var, resource and output match the ARM sections.
- @secure() stops a value being logged or shown in deployment history.
- Bicep works out dependencies automatically when one resource references another.
- Modules let you reuse one Bicep file inside another. The existing keyword references a resource that is already deployed.
3. Deploying
# Preview first az deployment group what-if -g rg-web --template-file main.bicep --parameters storageName=stsskaiser # Deploy az deployment group create -g rg-web --template-file main.bicep --parameters @main.parameters.json # PowerShell equivalent New-AzResourceGroupDeployment -ResourceGroupName rg-web -TemplateFile main.bicep -Mode Incremental
| Deploy to | CLI |
|---|---|
| Resource group | az deployment group create |
| Subscription (e.g. create resource groups) | az deployment sub create |
| Management group | az deployment mg create |
| Tenant | az deployment tenant create |
4. Running commands in Azure Cloud Shell
Azure Cloud Shell is a command line in your browser. Open it from the >_ icon in the portal (or at shell.azure.com), choose Bash or PowerShell, and you're already signed in as yourself. There's nothing to install or log in to.
- The Azure CLI, Azure PowerShell and common tools such as kubectl, Terraform, git and an editor are already installed and kept up to date.
- Both Bash and PowerShell run on Linux behind the scenes, and you can switch between them at any time.
- Sessions are temporary. To keep scripts and files between sessions, attach an Azure file share, which is mounted as clouddrive. Without one, files are lost when the session ends.
- A session closes after about 20 minutes without any activity.
Example
An admin at SS Kaiser is at a store with only a borrowed laptop, and needs to deploy a template urgently. They open Cloud Shell in the portal, upload main.bicep to their clouddrive folder and run az deployment group create. Nothing is installed on the laptop, and the file is still there next time they open Cloud Shell.
5. Export, convert and keep secrets safe
- Export template on a resource group generates a template of what exists now. The Deployments page of the resource group shows the exact template and parameters used for past deployments.
- az bicep decompile --file template.json converts ARM JSON to Bicep. az bicep build goes the other way.
- Never put passwords in parameter files. Reference a Key Vault secret instead. The vault must allow "Azure Resource Manager for template deployment".
"adminPassword": {
"reference": {
"keyVault": { "id": "/subscriptions/<id>/resourceGroups/rg-sec/providers/Microsoft.KeyVault/vaults/kv-sskaiser" },
"secretName": "vmAdminPassword" } }
Example
SS Kaiser opens stores in three new countries and needs the same web platform in each region. The team writes one Bicep file with a location parameter, runs what-if, then deploys it three times. Every environment is identical, and adding a fourth takes ten minutes.
Exam traps to remember
- Incremental is the default mode. Complete deletes anything not in the template.
- Exported templates leave out admin credentials, so you supply them at deployment.
- Use --no-wait (CLI) or -AsJob (PowerShell) so scripts do not block.
- Marketplace images may need terms accepted first with Set-AzMarketplaceTerms.
Test yourself: 4 check questions and exam-style practice questions on this topic are in the AZ-104 Azure Administrator app.
Practise in the app