Stacks What-If can show the changes made to a deployment stack's resources outside the stack since its last deployment. Below we will cover how this works and how to try it.
Following A closer look at noise reduction in Stacks What-If, this article covers how to use Stacks What-If to find configuration drift in a deployment stack. Drift detection has been a feature ask from Deployment Stacks customers since 2023 (Azure/deployment-stacks#117). In this article, we will cover what drift is, why noise reduction makes Stacks What-If useful for finding it, how to run a check, and how to read the result. For the full reference, see Use what-if to investigate configuration drift on Microsoft Learn.
What drift is
Configuration drift is when the current state of a resource differs from the template and parameters used to deploy it. For example, someone enables public network access on a storage account in the Azure portal, a script adds an inbound rule to a network security group, or a resource is deleted by hand. The stack doesn't restore any of these until its next deployment.
Deny settings block most of these changes. However, not every stack uses deny settings, and stacks that do can exclude principals and actions, which allows those principals, or anyone performing those actions, to make changes. Stacks What-If can show you those changes.
How Stacks What-If shows drift
Stacks What-If compares your template against the current state of the resources the stack manages. With noise reduction, Azure records a baseline after each successful deployment or update of the stack, and removes any difference that was already in that baseline from later results. When you run Stacks What-If with the same template, parameters, and stack settings that you last deployed, the result shows the drift: the changes made to the managed resources since that deployment (e.g. an edit in the Azure portal or from a script). Each difference is shown as the change that updating the stack would make to put the resource back to your template.
This depends on a baseline, which noise reduction records for stacks that you created or updated on or after August 13, 2026. Without a baseline, Stacks What-If still reports drift, but the result also includes noise. Updating the stack records a baseline, but it also restores the resources to match your template, so run Stacks What-If first if you want to see the drift that's already there.
Try it
The steps below use the Azure CLI. For the Stacks What-If step, we also show the Azure PowerShell command to help you get started.
We tested this on a resource group stack that manages a storage account with public network access disabled, a network security group that allows HTTPS, and an application security group:
param location string = resourceGroup().location
param storageName string = 'stdrift${uniqueString(resourceGroup().id)}'
resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: storageName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
minimumTlsVersion: 'TLS1_2'
allowBlobPublicAccess: false
allowSharedKeyAccess: false
publicNetworkAccess: 'Disabled'
}
}
resource nsg 'Microsoft.Network/networkSecurityGroups@2023-11-01' = {
name: 'nsg-app'
location: location
properties: {
securityRules: [
{
name: 'allow-https'
properties: {
priority: 100
direction: 'Inbound'
access: 'Allow'
protocol: 'Tcp'
sourceAddressPrefix: '*'
sourcePortRange: '*'
destinationAddressPrefix: '*'
destinationPortRange: '443'
}
}
]
}
}
resource asg 'Microsoft.Network/applicationSecurityGroups@2023-11-01' = {
name: 'asg-app'
location: location
}
We deployed it as a stack:
az stack group create \
--name "stack-app" \
--resource-group "rg-drift-demo" \
--template-file "main.bicep" \
--action-on-unmanage "detachAll" \
--deny-settings-mode "none"
Right after the deployment, a Stacks What-If run with the same template reported NoChange for all three resources.
Then we made three changes outside the stack, the same way someone could from a script or the Azure portal:
# Enable public network access on the storage account
az storage account update \
--name "<storage-account-name>" \
--resource-group "rg-drift-demo" \
--public-network-access Enabled
# Allow RDP from any source on the network security group
az network nsg rule create \
--resource-group "rg-drift-demo" \
--nsg-name "nsg-app" \
--name "allow-rdp" \
--priority 200 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--destination-port-ranges 3389
# Delete the application security group
az network asg delete \
--name "asg-app" \
--resource-group "rg-drift-demo"
Next, we ran Stacks What-If again with the unchanged template.
Azure CLI:
az stack-whatif group create \
--name "drift-check" \
--resource-group "rg-drift-demo" \
--stack-id "<stack-resource-id>" \
--template-file "main.bicep" \
--action-on-unmanage "detachAll" \
--deny-settings-mode "none" \
--retention-interval "PT3H"
Azure PowerShell:
New-AzResourceGroupDeploymentStackWhatIfResult `
-Name "drift-check" `
-ResourceGroupName "rg-drift-demo" `
-StackResourceId "<stack-resource-id>" `
-TemplateFile "main.bicep" `
-ActionOnUnmanage DetachAll `
-DenySettingsMode None `
-RetentionInterval "PT3H"
Here is the Azure CLI result. Azure PowerShell reported the same three changes.
Changes to Managed Resources:
Azure
+ /subscriptions/<subscription-id>/resourceGroups/rg-drift-demo/providers/Microsoft.Network/applicationSecurityGroups/asg-app [2023-11-01]
= Management Status: "managed"
= Deny Status: "none"
+ location: "westus2"
~ /subscriptions/<subscription-id>/resourceGroups/rg-drift-demo/providers/Microsoft.Network/networkSecurityGroups/nsg-app [2023-11-01]
= Management Status: "managed"
= Deny Status: "none"
~ properties.securityRules:
- 1:
{
"name": "allow-rdp",
"properties": {
"access": "Allow",
"destinationAddressPrefix": "*",
"destinationPortRange": "3389",
"direction": "Inbound",
"priority": 200,
"protocol": "Tcp",
"sourceAddressPrefix": "*",
"sourcePortRange": "*"
}
}
~ /subscriptions/<subscription-id>/resourceGroups/rg-drift-demo/providers/Microsoft.Storage/storageAccounts/stdriftdrpes7fyjvro4 [2023-05-01]
= Management Status: "managed"
= Deny Status: "none"
~ properties.publicNetworkAccess: "Enabled" => "Disabled"
Each of the three changes shows up as what updating the stack would do:
- The storage account shows
~ properties.publicNetworkAccess: "Enabled" => "Disabled". The value before=>is the current value, and the value after it is the value in the template. - The network security group shows the RDP rule as
- 1:underproperties.securityRules. Our template doesn't define that rule and it wasn't there at the last deployment, so updating the stack would remove it. - The application security group shows as
+because it was deleted, so updating the stack would create it again.
Stacks What-If didn't report any other differences. All three resources also have a myTag tag that an Azure Policy assignment in our test subscription adds at deployment time. Our template doesn't declare that tag, but it was already in the baseline, so Stacks What-If didn't report it.
To put the resources back, we updated the stack with the same template. A Stacks What-If run after that update reported NoChange for all three resources. Public network access was disabled again, the RDP rule was gone, and the application security group was back.
Reading the result
When you check a stack for drift, the change types mean the following:
- Modify (~) on a property that your template sets means the current value differs from your template.
- Delete (-) on a property or array item means the resource has a value that your template doesn't define and that wasn't there at the last deployment, e.g. a tag or a rule added outside the stack.
- Create (+) on a resource that the stack already manages means the resource no longer exists.
- NoChange (=) means Stacks What-If found no difference in the properties it evaluated.
- Delete or Detach on a whole resource usually comes from your template or your
actionOnUnmanagesetting rather than drift, e.g. a resource you removed from the template. - Unsupported, Ignore, or a diagnostic means Stacks What-If couldn't fully evaluate that part of the stack, so the result doesn't tell you whether it drifted.
Once you've confirmed a difference, you have two options. Update the stack with the same template to restore the resource, or update your template and parameters to keep the change and deploy that.
Things to keep in mind
- Use the same inputs. Run Stacks What-If with the same template, parameters, scope,
actionOnUnmanage, and deny settings as your last deployment. If any of these differ, the result also includes the effect of that difference. - It's a point-in-time check. Stacks What-If shows what differs when you run it. It doesn't watch for changes, and it doesn't show who made a change or when. For that, use the activity log or resource changes in Azure Resource Graph.
- Not every difference is drift. A resource provider can change a default value in a newer API version, and expressions that what-if can't evaluate (e.g.
reference(),utcNow(),newGuid(), and secure parameter values) can show up as changes. Confirm a difference against the live resource before you change anything. - Some changes can be missed. Stacks What-If doesn't evaluate unsupported resource types, and expansion limits or short-circuiting can leave resources out of the result. These show up as Unsupported or Ignore, or as a diagnostic on the result.
- Permissions. You need permission to read the stack and the resources it manages, and permission to create the result (
Microsoft.Resources/deploymentStacksWhatIfResults/write). The built-in Azure Deployment Stack Contributor role can create the result, but it also lets you create and update stacks. To limit someone to Stacks What-If, use a custom role with just these permissions.
Feedback
If you need more than a point-in-time check, tell us what you would use it for in Azure/deployment-stacks#117. If Stacks What-If reports a difference that you didn't make, open an issue in Azure/deployment-stacks and include the resource type, the property path as it appears in the output, and the correlation ID of the result.
Learn more
- Use what-if to investigate configuration drift. The Microsoft Learn section on drift, with the steps and how to read the result.
- Noise reduction in deployment stack what-if results. How the baseline works, and what it doesn't filter.
- A closer look at noise reduction in Stacks What-If. The earlier post on noise reduction.
- Protect managed resources. Deny settings for deployment stacks.
- az stack-whatif command reference. The Azure CLI command group for Stacks What-If.
- New-AzResourceGroupDeploymentStackWhatIfResult. The Azure PowerShell cmdlet reference.