Lesson 4 of 5 ARM Templates and Bicep Files

Deploying Templates at the Right Scope and Mode

This lesson explains deployment scopes and commands, incremental and complete modes, what-if and deployment history.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the deployment command for each scope.
  • Distinguish between incremental and complete deployment modes.
  • Explain how what-if and deployment history help prevent and diagnose problems.

Deployment tools and scopes

The deployment command depends on the target scope. The following table lists the Azure CLI and Azure PowerShell commands for each scope; both accept .json and .bicep files and parameter files.

ScopeAzure CLIAzure PowerShellTypical use
Resource groupaz deployment group createNew-AzResourceGroupDeploymentMost workload resources
Subscriptionaz deployment sub createNew-AzSubscriptionDeploymentResource groups, policy and role assignments, budgets
Management groupaz deployment mg createNew-AzManagementGroupDeploymentPolicies and roles across subscriptions
Tenantaz deployment tenant createNew-AzTenantDeploymentManagement groups
az deployment group create \
  --resource-group rg-app \
  --template-file main.bicep \
  --parameters @main.parameters.json vmSize=Standard_D2s_v5

In the portal, Deploy a custom template accepts a quickstart template, a template built in the editor, or an uploaded file, and generates a form from its parameters. Template specs store templates as Azure resources with versions and RBAC so that teams deploy approved templates from the portal.

Match the pairs

Which deployment command?

Exam skills 3.1.4

Match each deployment with the Azure CLI command for its scope.

Log in to try this activity Interactive activities are free with an account.

Incremental and complete modes

Resource group deployments run in one of two modes. In Incremental mode, the default, Resource Manager creates resources that do not exist and updates those whose properties differ, and it leaves resources that are in the resource group but not in the template untouched. In Complete mode, Resource Manager also deletes resources in the resource group that are not in the template (resources skipped by a false condition are not deleted). Complete mode is selected with --mode Complete.

Incremental
Resources in templateCreated or updated
Resources not in templateLeft unchanged
DefaultYes
Choose when Adding or updating resources safely
Complete
Resources in templateCreated or updated
Resources not in templateDeleted
DefaultNo
Choose when Making a resource group exactly match a template

What-if (az deployment group what-if or the -WhatIf switch) previews the result without making changes, listing resources to create, modify, delete or ignore. It should always be run before a Complete-mode deployment. Deployment stacks are a newer way to manage a set of resources as a unit, with deny settings that protect them.

The following diagram compares Complete and Incremental deployment modes, which explains why a template deployment deleted a manually created key vault.

Why deploying a template deleted a manually created key vault. The runbook used Complete mode, which removes resources in the resource group that are not in the template. Incremental mode, the default, leaves them alone; adding the vault to the template and running what-if first show and prevent such deletions.
The scenario is illustrative.
Sort into groups

What happens to each resource?

Exam skills 3.1.4

A resource group holds a VNet, a VM and a key vault. The template defines the VNet and the VM only. Decide what happens in each case.

Log in to try this activity Interactive activities are free with an account.

Deployment history and troubleshooting

Every deployment is stored on the scope's Deployments page with its template, parameters, outputs and per-resource operations. Failed deployments show error codes such as InvalidTemplate (syntax or validation), QuotaExceeded, SkuNotAvailable or RequestDisallowedByPolicy (an Azure Policy Deny). A deployment can be redeployed from history, and resource groups keep up to 800 deployments, deleting the oldest automatically. A deployment name reused in the same scope overwrites the previous history entry.

Match the pairs

Reading deployment errors

Exam skills 3.1.4

Match each deployment error with its cause.

Log in to try this activity Interactive activities are free with an account.
Worked example

An unexpected deletion

An engineer deploys a template containing a storage account and an App Service plan to rg-shared, which also contains a key vault created manually. After deployment, the key vault has been deleted. The engineer used the command shown in the runbook. What caused the deletion, and how should it be prevented?

The runbook's command used Complete mode, which deleted the key vault because it was not in the template. Use the default Incremental mode, or add the key vault to the template, and run what-if before deploying to see deletions in advance. A CanNotDelete lock on the key vault would also have blocked the deletion. The key vault can be recovered from soft delete within its retention period.

Check yourself

Which deployment scope is needed to create resource groups from a template?

Show answer

Subscription scope, for example az deployment sub create.

Exam Tips

Remember for the exam

  • Incremental is the default and never deletes; Complete deletes resources not in the template.
  • Run what-if to preview changes.
  • Create resource groups from a template with a subscription-scope deployment.
  • RequestDisallowedByPolicy means an Azure Policy assignment blocked the deployment.