Automation

Azure Deployment Stacks: validate action-on-unmanage before removing a production resource

A production runbook for qualifying an Azure Deployment Stacks update, separating detach from delete, testing deny settings, and keeping an explicit rollback path.

11 Oct 2026 azuredeployment-stacksbicepinfrastructure-as-codeautomationsecuritydeny-settingsazure-clirunbookvalidationrollbackproduction

A pull request removes a storage account from a platform Bicep file. The intent is to transfer ownership to another team without interrupting the service. The pipeline then updates the Azure Deployment Stack with deleteResources. The resource is not merely absent from the template: it becomes unmanaged and Azure is instructed to delete it.

The risk is broader than one incorrect CLI option. An operator may read denyDelete and assume the resource is protected, overlook a child created implicitly by a managed service, or force an out-of-sync stack without reviewing its actual resource list. Successful template validation is not a deletion preview either.

The running case is a resource group-scoped stack that manages an application, its identity, a storage account, and monitoring settings. This runbook must end with a clear decision: promote the update, deliberately detach the resource, correct the contract, or restore the previous template and configuration.

Freeze the management contract before reading the diff

Document the actual target stack, source commit, parameters, and the two controls that govern its behavior: action-on-unmanage and deny settings. Do not infer these values from the intended pipeline; preserve the Azure response.

yaml deployment-stack-contract.yml
stack:
scope: resourceGroup
resource_group: rg-orders-prod
name: stack-orders-prod
source_commit: 4f8c2d1

change:
removed_resource: Microsoft.Storage/storageAccounts/stordersprod
intent: transfer_management
expected_data_effect: none

controls:
action_on_unmanage: detachAll
deny_mode: denyDelete
apply_to_child_scopes: false
excluded_principals: [platform-pipeline-group]

decision_owner: platform-on-call
rollback_owner: cloud-platform

The word “remove” is ambiguous. Removing from the template, removing from management, and deleting in Azure are three different actions. The review must name the expected one.

Capture the stack and its actual managed resources

Export state before any update. Keep the full stack response, managed resource IDs, and exported template. The ID manifest is the basis for the operational diff.

bash 01-capture-stack.sh
set -euo pipefail

RG="rg-orders-prod"
STACK="stack-orders-prod"

az stack group show --resource-group "$RG" --name "$STACK" --output json > stack-before.json

jq -r '.resources[]?.id' stack-before.json | sort -u > managed-before.txt

az stack group export --resource-group "$RG" --name "$STACK" --output json > template-before.json

sha256sum stack-before.json managed-before.txt template-before.json > evidence.sha256

Compare managed-before.txt with resources explicitly declared in the compiled Bicep. An implicitly created resource, such as infrastructure behind a managed service, is not automatically managed or protected by the stack deny assignment. Rollback must therefore distinguish what the stack owns from what the platform creates around it.

Read action-on-unmanage as a destruction policy

During an update, every resource removed from the template becomes unmanaged. detachAll leaves it in place and breaks the management link. deleteResources deletes managed resources that become unmanaged, but not resource groups. deleteAll also includes affected managed resource groups.

This option belongs to the change, not to a hidden global pipeline preference. For an ownership transfer, detachAll matches the intent. For deliberate cleanup, deleteResources can be correct only when every candidate resource is identified, empty or backed up according to its contract, and explicitly approved for destruction.

text action-on-unmanage-matrix.txt
Intent: transfer management without deleting Azure state
action-on-unmanage: detachAll
evidence: target owner, import plan, monitoring continuity

Intent: retire explicitly managed resources
action-on-unmanage: deleteResources
evidence: exact IDs, dependency checks, data disposition, approval

Intent: retire a complete managed environment including resource groups
action-on-unmanage: deleteAll
evidence: isolated scope, recovery proof, explicit change authorization

Unknown intent or incomplete inventory
decision: stop
action-on-unmanage: detachAll only in a controlled recovery plan

Do not replace a dangerous choice with detachAll and consider the work complete. A detached resource keeps running but leaves the stack update and protection lifecycle. Assign an owner and a new IaC path immediately.

Verify what deny settings really protect

denyDelete blocks deletion of explicitly managed resources; denyWriteAndDelete also blocks control-plane writes. These protections do not cover the data plane, implicitly created resources, or every scope change. Deleting the parent resource group can bypass protection from a stack created inside that resource group.

Record the mode, child-scope application, excluded actions, and excluded principals. Test with the real pipeline identity and a non-excluded identity. An overbroad exception turns the deny assignment into decorative documentation; overbroad protection can block a legitimate child deployment.

For a production boundary, placing the stack at a parent scope and declaring the resource group in its template provides a stronger boundary than storing the stack inside the resource group it is meant to protect.

Validate without confusing syntax with impact

The validation command checks the request, template, parameters, and, depending on the selected level, provider controls. It does not replace a review of resources leaving management. What-if is not supported for deployment stacks. A standard ARM what-if can clarify template creates and modifications, but it does not model stack-specific cleanup.

bash 02-validate-and-review.sh
set -euo pipefail

RG="rg-orders-prod"
STACK="stack-orders-prod"
TEMPLATE="main.bicep"
PARAMS="prod.bicepparam"

az stack group validate --resource-group "$RG" --name "$STACK" --template-file "$TEMPLATE" --parameters "$PARAMS" --action-on-unmanage detachAll --deny-settings-mode denyDelete --validation-level Provider --output json > validation.json

az deployment group what-if --resource-group "$RG" --template-file "$TEMPLATE" --parameters "$PARAMS" --result-format FullResourcePayloads --output json > arm-what-if.json

git diff --unified=20 origin/main...HEAD -- '*.bicep' '*.bicepparam' > source-change.diff

Cross-check declarations removed in source-change.diff, ARM changes, and managed-before.txt. For every resource that may leave management, record the full Azure ID, target owner, and intended action in the change ticket. Human review covers each management exit, not only the line count.

Canary the policy before production

Replay the same stack in an isolated scope with a data-free canary resource. First pass: create it with the intended deny settings. Second pass: remove it from the template with detachAll and prove that it still exists but no longer appears in the managed list. Third pass, only when deletion is intended: recreate the canary, use deleteResources, and verify that it disappears.

The canary must also cover an excluded principal and a non-excluded principal. Verify that manual deletion is denied for the latter, that the pipeline can run the bounded change, and that an exclusion grants no permissions by itself: it removes the deny-setting effect but does not replace RBAC.

Handle an out-of-sync stack without forcing blind

If Azure reports that the stack resource list is out of sync, stop the deletion. First reapply the template and parameters currently running in production to rebuild a coherent list. Compare it with inventory, Azure Resource Graph, activity logs, and the repository.

--bypass-stack-out-of-sync-error is an exception decision after reconciliation, never a permanent pipeline flag. The bypass removes a barrier designed to prevent accidental destruction; it does not repair resource ownership.

Promote, detach, or roll back

yaml deployment-stack-decision.yml
promote:
when:
  - managed_resource_diff_reviewed
  - action_on_unmanage_matches_intent
  - deny_settings_tested_with_real_identities
  - canary_reproduces_expected_behavior
  - rollback_template_and_parameters_are_available

detach:
when:
  - resource_must_survive
  - target_owner_and_iac_path_are_ready
action_on_unmanage: detachAll

hold:
when:
  - stack_inventory_is_out_of_sync
  - implicit_or_child_resources_are_unmapped
  - data_disposition_is_unknown

rollback:
actions:
  - restore_previous_template_and_parameters
  - restore_previous_action_on_unmanage_and_deny_settings
  - reapply_stack_with_detachAll_until_inventory_is_reconciled
  - verify_application_and_monitoring_state

After promotion, export the stack again and compare managed IDs. For a detached resource, verify existence, new ownership, monitoring, and import into the next IaC state. For an approved deletion, check dependencies and activity logs, then run an application validation.

If the outcome is ambiguous, restore the previous template and parameters with detachAll, then reconcile resources before another attempt. Recreating a deleted resource is not always a rollback: data, identities, endpoints, or identifiers may have changed.

Conclusion

Azure Deployment Stacks turns a resource’s absence from the template into an operational decision. Safety comes less from one flag than from a reviewable contract across managed inventory, action-on-unmanage, deny settings, identities, and data disposition.

The runbook outcome is deliberately binary: promote a change where every exiting resource is understood, detach to a ready owner, or return to the previous contract. While inventory remains uncertain, deletion is never cleanup.