Overview and What You Will Learn
An ARM template is a JSON file that describes the Azure resources you want, and Azure Resource Manager works out how to create or update them to match. In this lab you write a real template for a storage account, validate it, preview the changes with what-if, deploy it, and reuse it with different parameter files to build two environments from one definition.
By the end you will be able to:
- Read and write the five sections of an ARM template
- Separate environment differences into parameter files
- Run validate and what-if before every deployment
- Explain why re-running a template does not create duplicates
- Choose between Incremental and Complete deployment modes without deleting things by accident
Why This Matters in Production
Infrastructure that exists only as a series of portal clicks cannot be reviewed, repeated, or recovered reliably. If a resource group is deleted or a new region is needed, the team rebuilds from memory and an out-of-date wiki page, and the copy never quite matches the original.
A template turns that into a file in version control. A platform team at a company like Atlassian can review an infrastructure change in a pull request, preview it with what-if, and deploy the same definition to dev, test, and production, so the environments differ only by parameter values.
Core Principles
ARM templates are declarative: you describe the end state and Azure decides the steps and the order. An imperative script says "create a VNet, then a subnet, then a VM". A template says "this VNet, subnet, and VM should exist", and ARM handles dependencies, retries, and updates.
A template has up to five sections:
- parameters: Values you supply at deploy time, such as names, regions, and SKUs.
- variables: Values computed once inside the template.
- resources: The Azure resources to create or update.
- outputs: Values returned after deployment, such as an endpoint URL.
- functions: Optional reusable expressions, for advanced templates.
Anything wrapped in square brackets, such as [parameters('location')], is an expression that ARM evaluates at deployment time.
parameter file"]:::person -->|validate| V["ARM validation"]:::tool V -->|what-if| W["Change preview"]:::app W -->|deploy| D["Deployment"]:::tool D -->|creates or updates| R[("Azure resources")]:::cloud classDef person fill:#EDE9FE,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef app fill:#FFEDD5,color:#1E293B classDef cloud fill:#D1FAE5,color:#1E293B
Because the template states the desired result, deployments are idempotent. Running the same template again updates anything that drifted and leaves everything else alone.
RememberBicep is a friendlier language that compiles to ARM templates, and it is the recommended way to author new infrastructure. Learning the ARM structure first makes Bicep easy, because the sections and concepts are identical.
Detailed Step-by-Step Practical Lab
Step 1: Create a resource group.
RG=rg-arm-lab-mumbaiaz group create --name $RG --location centralindiaStep 2: Write the template. Save this as storage-template.json. It takes a name, a region, a SKU, and an environment tag, and it turns on secure defaults for the account. JSON templates cannot contain ordinary comments, so the explanation is below the block.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "storageAccountName": { "type": "string", "minLength": 3, "maxLength": 24, "metadata": { "description": "Globally unique name, lowercase letters and digits only" } }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]" }, "skuName": { "type": "string", "defaultValue": "Standard_LRS", "allowedValues": ["Standard_LRS", "Standard_GRS", "Standard_ZRS"] }, "environment": { "type": "string", "defaultValue": "dev" } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2023-01-01", "name": "[parameters('storageAccountName')]", "location": "[parameters('location')]", "sku": { "name": "[parameters('skuName')]" }, "kind": "StorageV2", "tags": { "environment": "[parameters('environment')]" }, "properties": { "minimumTlsVersion": "TLS1_2", "allowBlobPublicAccess": false, "supportsHttpsTrafficOnly": true } } ], "outputs": { "blobEndpoint": { "type": "string", "value": "[reference(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))).primaryEndpoints.blob]" }, "storageAccountId": { "type": "string", "value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]" } }}Reading the template: the location parameter defaults to the resource group's region, allowedValues rejects any SKU you did not approve, and minLength and maxLength catch an invalid account name before deployment starts. The properties block disables anonymous blob access and old TLS versions for every account built from this file.
Step 3: Write one parameter file per environment. Storage account names are globally unique, so change the suffix if a name is taken.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "storageAccountName": { "value": "stdevlabrahul01" }, "skuName": { "value": "Standard_LRS" }, "environment": { "value": "dev" } }}Save that as dev.parameters.json. Then create prod.parameters.json with the same structure but these values: name stprodlabrahul01, SKU Standard_GRS, environment prod.
Step 4: Validate the template. Validation checks syntax, parameter rules, and basic resource definitions without creating anything.
az deployment group validate \ --resource-group $RG \ --template-file storage-template.json \ --parameters @dev.parameters.jsonStep 5: Preview the change with what-if. What-if compares the template with what exists now and lists each resource as create, modify, delete, or no change.
az deployment group what-if \ --resource-group $RG \ --template-file storage-template.json \ --parameters @dev.parameters.jsonStep 6: Deploy the dev environment.
az deployment group create \ --name dev-storage \ --resource-group $RG \ --template-file storage-template.json \ --parameters @dev.parameters.jsonStep 7: Prove idempotency. Run what-if again against the same parameters. Nothing should change, because the resource already matches the template.
az deployment group what-if \ --resource-group $RG \ --template-file storage-template.json \ --parameters @dev.parameters.jsonStep 8: Reuse the same template for production. Only the parameter file changes, yet the result is a second account with geo-redundant storage and a different tag.
az deployment group create \ --name prod-storage \ --resource-group $RG \ --template-file storage-template.json \ --parameters @prod.parameters.json az storage account list --resource-group $RG \ --query "[].{name:name, sku:sku.name, env:tags.environment}" --output tableStep 9: Read the outputs.
az deployment group show --resource-group $RG --name dev-storage \ --query properties.outputsStep 10: See why Complete mode is dangerous. Incremental mode, the default, leaves resources that are not in the template alone. Complete mode removes them. Run only a what-if, and notice that the production account is marked for deletion because the dev parameters do not mention it.
az deployment group what-if \ --resource-group $RG \ --template-file storage-template.json \ --parameters @dev.parameters.json \ --mode CompleteStep 11: Look at the Bicep equivalent. Decompiling shows how the same definition reads in Bicep.
az bicep decompile --file storage-template.jsonStep 12: Clean up.
az group delete --name $RG --yes --no-waitProduction Best Practices & Common Pitfalls
- Keep templates and parameter files in version control: A template on one laptop is as fragile as the portal clicking it replaced. Review changes through pull requests.
- Run validate and what-if in your pipeline: Make the what-if output part of the review so reviewers see exactly what will change before approval.
- One template, many parameter files: Environments should differ only in parameter values, never in the template itself.
- Never put secrets in parameters or outputs: Deployment history stores them. Use
securestringparameters or Azure Key Vault references, and never output a secret. - Use explicit
dependsOnonly when needed: ARM infers dependencies from resource references. AdddependsOnwhen two resources are related but one does not reference the other. - Protect important resources: Add resource locks, and keep the
apiVersionvalues current as services evolve. - Prefer Bicep for new work: It compiles to the same ARM template, with less syntax and better tooling support.
Common MistakeDeploying to a shared resource group in Complete mode. Every resource that is not in the template is deleted, including things other teams created. Use Incremental unless you have read the what-if output and want the cleanup.
Common MistakeHard-coding a region, such as
centralindia, inside the resources section. The template then cannot be reused for another region without editing it. Use alocationparameter instead.
SecurityAnything returned in
outputsis visible in the deployment history to anyone who can read it. Output resource IDs and endpoints, never keys or connection strings.
TipStorage account names must be globally unique. Build them with
uniqueString(resourceGroup().id)in a variable if you want names that never collide.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az deployment group validate |
Check a template without deploying |
az deployment group what-if |
Preview creates, changes, and deletes |
az deployment group create |
Deploy a template to a resource group |
az deployment group list |
List past deployments in a resource group |
az deployment operation group list |
Show per-resource status for a deployment |
az deployment group export |
Export a resource group as a template |
az bicep build |
Compile a Bicep file to an ARM template |
az bicep decompile |
Convert an ARM template to Bicep |
| Error | Likely cause |
|---|---|
StorageAccountAlreadyTaken |
The name is not globally unique |
InvalidTemplate |
Syntax error or a wrong function in an expression |
RequestDisallowedByPolicy |
An Azure Policy denied the resource |
ResourceNotFound during deploy |
A missing dependency, so add a reference or dependsOn |